获取天气预报的接口

以前的几个天气预报的接口没法用了,新找了两个接口
一个接口启用了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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值