获取接口数据:
1.引入requsets包 这个大多数编辑器都有这个包,没有的话,那就自己去下!!!
3.通过请求拿到数据
4.再把这些数据转换为字典,那么想要整理这些数据或者输出就可以通过字典操作!
具体代码如下:
# 引入请求包
import requests
url = 'http://api.map.baidu.com/telematics/v3/weather?location=郑州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
# 1.通过请求拿回数据
response = requests.get(url)
print(response)
# 2.将数据转换为字典
result = response.json()
date = result['date']
print(date)
下面是一个例子,通过获取接口数据,进行的一个天气预报查询的小程序!
def today():
if currentCity == city:
weather = weather_data[0]
pm = inf['pm25']
pm = int