功能讲解:
1.根据你的ip地址,定位你所在的城市,然后把城市参数传给city_weather函数,向服务器发送请求,得到天气结果
2.直接输入你要查询的城市名,就可获取天气情况。
代码:
import requests,json
def city_weather(city_name):
request=requests.get('http://api.map.baidu.com/telematics/v3/weather?location={}&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'.format(city_name)) # 向网站发送Get请求,返回的是请求状态,可以判断请求是否成功
print(request)
print(request.text)
json_str=request.text # 返回的是请求状态和要解析的json字符串
json_dict=json.loads(json_str) # 把json字符串转化为字典
data_dict=json_dict