以前的几个天气预报的接口没法用了,新找了两个接口
一个接口启用了gzip,用代码处理的时候,需要注意下
json-handle的插件地址,可以很方便的解析json http://jsonhandle.sinaapp.com/
代码1
代码2
一个接口启用了gzip,用代码处理的时候,需要注意下
json-handle的插件地址,可以很方便的解析json http://jsonhandle.sinaapp.com/
代码1
#encoding=utf8
import urllib
import urllib.request
import http.cookiejar
import json
import gzip
User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent
weatherUrl='http://wthrcdn.etouch.cn/weather_mini?citykey=101110101'
urlFile = urllib.request.urlopen(weatherUrl)
jsonstr = gzip.decompress(urlFile.read()).decode("utf-8")
urlFile.close()
print("load page success")
#print(jsonstr)
jsondata=json.loads(jsonstr)
weather=jsondata['data']
'''
print("城市:"+weather['city'])
print("温度:"+weather['wendu'])
print("明天天气:"+weather['forecast'][0]['type'])
print("明天最高温度:"+weather['forecast'][0]['high'])
print("明天最低温度:"+weather['forecast'][0]['low'])
print("明天最低温度:"+weather['forecast'][0]['low'])
'''
printStr="当前温度"+weather['wendu']+",明天预报,"+weather['forecast'][0]['high']+weather['forecast'][0]['low']+","+weather['forecast'][0]['fengli']+weather['forecast'][0]['fengxiang']
print(printStr)
代码2
#encoding=utf8
import urllib
import urllib.request
import http.cookiejar
import json
User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent
weatherUrl='http://www.weather.com.cn/data/sk/101110102.html'
urlFile = urllib.request.urlopen(weatherUrl)
jsonstr = urlFile.read()
urlFile.close()
jsonstr = jsonstr.decode('utf-8',errors='ignore')
print("load page success")
print(jsonstr)
jsondata=json.loads(jsonstr)
weather=jsondata['weatherinfo']
print("城市:"+weather['city'])
print("当前温度:"+weather['temp'])
print("风:"+weather['WD']+weather['WS'])
print("湿度:"+weather['SD'])
print("更新时间"+weather['time'])
printStr=weather['city']+"的当前温度是"+weather['temp']+","+weather['WD']+weather['WS']+",湿度"+weather['SD']+"。最后更新时间"+weather['time']
print(printStr)