添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

这个简单的练手内容其实也不用一分钱。

后面熟练了可以花钱去调取其他更有意义的接口数据来做分析。

请求参数中cityid、token我们可以通过产品说明页的链接下载下来查看感兴趣的城市。

点击我获取cityid对照表 点击我了解如何获取token

具体的参数,官方详情页都给出了详细的介绍,在这里不再赘述。

我们通过详情页的介绍可以了解各参数的意义以及调用方法。

现在我们有了API接口的AppCode码,token,cityid,调用示例也给了我们。

开始上手用python来获取数据。首先我们要具备python的环境,在这里不再具体写环境的搭建,我使用的是python3的环境Anaconda3。

我们在桌面建一个demo-api文件夹,打开Anaconda prompt,其实就是win下的cmd,prompt是Anaconda自带的命令行工具。我们进入到桌面自己新建的目录。

使用jupyter notebook命令进入我们的python环境,其会打开我们的默认浏览器作为我们的python环境。

首先,要发送请求获取数据。因此我们要导入requests库。写入url地址,请求参数,发起请求。

cityId = '''394'''
token = '''your token number'''
import requests
url = 'http://freecityid.market.alicloudapi.com/whapi/json/alicityweather/briefforecast3days'
payload = {'cityId': cityId, 'token': token}
headers = {'Authorization': 'APPCODE {}'.format(appcode)}
r = requests.post(url, params=payload, headers=headers)

现在我们输入r,返回的是:

<Response [200]> 

别着急,200是个好数字,说明我们请求成功。
现在来查看r的信息:

r.content
b'{"code":0,"data":{"city":{"cityId":394,"counname":"\xe4\xb8\xad\xe5\x9b\xbd","name":"\xe6\xb4\x9b\xe9\x98\xb3\xe5\xb8\x82","pname":"\xe6\xb2\xb3\xe5\x8d\x97\xe7\x9c\x81","timezone":"8"},"forecast":[{"conditionDay":"\xe6\x99\xb4","conditionIdDay":"0","conditionIdNight":"30","conditionNight":"\xe6\x99\xb4","predictDate":"2018-09-21","tempDay":"28","tempNight":"15","updatetime":"2018-09-21 20:14:00","windDirDay":"\xe8\xa5\xbf\xe5\x8c\x97\xe9\xa3\x8e","windDirNight":"\xe8\xa5\xbf\xe9\xa3\x8e","windLevelDay":"4-5","windLevelNight":"3-4"},{"conditionDay":"\xe6\x99\xb4","conditionIdDay":"0","conditionIdNight":"30","conditionNight":"\xe6\x99\xb4","predictDate":"2018-09-22","tempDay":"29","tempNight":"14","updatetime":"2018-09-21 20:14:00","windDirDay":"\xe8\xa5\xbf\xe5\x8c\x97\xe9\xa3\x8e","windDirNight":"\xe8\xa5\xbf\xe9\xa3\x8e","windLevelDay":"3","windLevelNight":"3"},{"conditionDay":"\xe6\x99\xb4","conditionIdDay":"0","conditionIdNight":"31","conditionNight":"\xe5\xa4\x9a\xe4\xba\x91","predictDate":"2018-09-23","tempDay":"27","tempNight":"14","updatetime":"2018-09-21 20:14:00","windDirDay":"\xe4\xb8\x9c\xe5\x8c\x97\xe9\xa3\x8e","windDirNight":"\xe5\x8c\x97\xe9\xa3\x8e","windLevelDay":"3","windLevelNight":"3"}]},"msg":"success","rc":{"c":0,"p":"success"}}'

这都是什么呢 ,别着急 ,官方文档说返回的是json文件,因此我们导入json包,用 json 包的字符串处理功能(loads)解析返回内容,结果存入 content_json

import json
content_json=json.loads(r.content)

查看结果:

这样,我们第一步工作做完了。如果我们想获取更多的城市天气信息,采用一个个去输入城市id一个个运行显然是违背了利用计算机的强大来简化我们工作的初衷。

必须利用计算机来让我们可以偷个懒喝杯茶还能获取满意的结果才行,我们的前辈们告诉我们可以通过定义一个函数来循环运行我们的测试代码,从而实现一次获取多个城市的天气数据。

代码如下:

def get_df(cityId,cityname_dict, appcode):
    url = 'http://freecityid.market.alicloudapi.com/whapi/json/alicityweather/briefforecast3days'
    payload = {'cityId': cityId, 'token': token}
    headers = {'Authorization': 'APPCODE {}'.format(appcode)}
    r = requests.post(url, params=payload, headers=headers)
    content_json = json.loads(r.content)
    df = pd.DataFrame(content_json['data']['forecast'])
    df['cityname'] = cityname_dict[cityId]
    return df

我们为函数增加了一个输入参数,即cityname_dict

它是一个字典,每一项分别包括城市代码,和对应的城市名称。

根据我们输入的城市代码,函数就可以自动在结果数据框中添加一个列,注明对应的是哪个城市。

当我们获取多个城市的数据时,某一行的数据说的是哪个城市,就可以一目了然。

反之,如果只给你看城市代码,你很快就会眼花缭乱,不知所云了。

但是,只有上面这一个函数,还是不够高效。

我们再定义一个函数,来遍历cityId

def get_dfs(cityname_dict, appcode):
    dfs = []
    for cityId in cityname_dict:
        dfs_times = []
        temp_df = get_df(cityId, cityname_dict, appcode)
        dfs_times.append(temp_df)
        city_df = pd.concat(dfs_times,ignore_index=True)
        dfs.append(city_df)
    return dfs

输入要查询城市的ID和名称字典:

cityname_dict = {"2884":"丽江","379":"郑州","394":"洛阳","285255":"大理"}

调用函数get_dfs:

dfs = get_dfs(cityname_dict, appcode)

查看dfs信息:

我们发现我们的时间参数updatetime不是时间类型,需要转换成时间类型,温度不是数字类型,也需要我们处理:

df.updatetime = pd.to_datetime(df.updatetime)