python+requests发送post请求

1、使用requests发送post请求

# 导入request
import requests

# 接口地址
url = 'http://v.juhe.cn/historyWeather/weather'
# 发送的请求参数
dd = {'city_id':'1154','key':'61e0c8a6d9614382afbaa7f35dbd3ec6','weather_date':'2020-7-15'}
# 使用requests发送post请求
re = requests.post(url,data=dd)
# 定义js保存返回的json
js = re.json()
# 定义变量保存返回的参数
a = js['reason']
b = js['result']['city_name']
c = js['error_code']
# 打印返回的json,返回说明,错误码,城市名
print('返回的json:',js,'\n返回说明:',a,'\n城市名',b,'\n错误码:',c)

运行结果:

返回的json: {'reason': '查询成功', 'result': {'city_id': '1154', 'city_name': '沭阳', 'weather_date': '2020-07-15', 'day_weather': '晴', 'night_weather': '阴', 'day_temp': '29℃', 'night_temp': '21℃', 'day_wind': '南风', 'day_wind_comp': '<3级', 'night_wind': '西南风', 'night_wind_comp': '<3级', 'day_weather_id': '00', 'night_weather_id': '02'}, 'error_code': 0}

返回说明: 查询成功

城市名 沭阳

错误码: 0

拿结果与接口文档对比即可