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
拿結果與接口文檔對比即可