python 天气预报
import json
import requests
获取多地一周天气
city = "上海"
url = 'http://wthrcdn.etouch.cn/weather_mini?city={}'.format(city)
f=requests.get(url)
print(f.text
{"data":{"yesterday":{"date":"9日星期日","high":"高温 7℃","fx":"北风","low":"低温 3℃","fl":"<![CDATA[2级]]>","type":"多云"},"city":"上海","forecast":[{"date":"10日星期一","high":"高温 9℃","fengli":"<![CDATA[2级]]>","low":"低温 2℃","fengxiang":"北风","type":"霾"},{"date":"11日星期二","high":"高温 7℃","fengli":"<![CDATA[3级]]>","low":"低温 0℃","fengxiang":"北风","type":"晴"},{"date":"12日星期三","high":"高温 8℃","fengli":"<![CDATA[2级]]>","low":"低温 2℃","fengxiang":"东南风","type":"晴"},{"date":"13日星期四","high":"高温 7℃","fengli":"<![CDATA[3级]]>","low":"低温 2℃","fengxiang":"北风","type":"多云"},{"date":"14日星期五","high":"高温 8℃","fengli":"<![CDATA[2级]]>","low":"低温 3℃","fengxiang":"东风","type":"阴"}],"ganmao":"感冒多发期,适当减少外出频率,适量补充水分,适当增减衣物。","wendu":"8"},"status":1000,"desc":"OK"}
jsons=json.loads(f.text)
print(jsons['data']['forecast'])
[{'date': '10日星期一', 'high': '高温 9℃', 'fengli': '<![CDATA[2级]]>', 'low': '低温 2℃', 'fengxiang': '北风', 'type': '霾'}, {'date': '11日星期二', 'high': '高温 7℃', 'fengli': '<![CDATA[3级]]>', 'low': '低温 0℃', 'fengxiang': '北风', 'type': '晴'}, {'date': '12日星期三', 'high': '高温 8℃', 'fengli': '<![CDATA[2级]]>', 'low': '低温 2℃', 'fengxiang': '东南风', 'type': '晴'}, {'date': '13日星期四', 'high': '高温 7℃', 'fengli': '<![CDATA[3级]]>', 'low': '低温 2℃', 'fengxiang': '北风', 'type': '多云'}, {'date': '14日星期五', 'high': '高温 8℃', 'fengli': '<![CDATA[2级]]>', 'low': '低温 3℃', 'fengxiang': '东风', 'type': '阴'}]
for i in jsons['data']['forecast']:
print(i['date'])
print(i['high'])
print(i['low'])
print(i['fengli'])
print(i['type'])
10日星期一
高温 9℃
低温 2℃
<![CDATA[2级]]>
霾
11日星期二
高温 7℃
低温 0℃
<![CDATA[3级]]>
晴
12日星期三
高温 8℃
低温 2℃
<![CDATA[2级]]>
晴
13日星期四
高温 7℃
低温 2℃
<![CDATA[3级]]>
多云
14日星期五
高温 8℃
低温 3℃
<![CDATA[2级]]>
阴
获取本地今天天气
url = 'http://www.weather.com.cn/data/sk/101020100.html'
f=requests.get(url)
f.encoding = 'utf-8'
print(f.text)
{"weatherinfo":{"city":"上海","cityid":"101020100","temp":"23.5","WD":"东北风","WS":"小于3级","SD":"80%","AP":"1006.4hPa","njd":"2903","WSE":"<3","time":"17:00","sm":"1.1","isRadar":"1","Radar":"JC_RADAR_AZ9210_JB"}}