python 天气预报获取
2010 年 3 月 30 日
通过对地名的编码获取,通过编码获取到天气信息 源码如下:
# -*- encoding: utf-8 -*- import urllib2 from cityCode import city import json location = raw_input('请输入你需要查询的城市名字:') citycode="" # 城市编码获取 try: citycode =city[location] except: print "not Found" url= "http://www.weather.com.cn/data/cityinfo/"+citycode+".html"#构造网址 # 发起查询 weather_json_string = urllib2.urlopen(url).read() # 结果分析 weather = json.loads(weather_json_string) print weather # 结果打印 print weather['weatherinfo']['city'] print weather['weatherinfo']['ptime'] print weather['weatherinfo']['cityid'] print weather['weatherinfo']['temp2'] print weather['weatherinfo']['temp1'] print weather['weatherinfo']['weather']
运行结果截图:
欢迎关注公众号 「网罗开发」 ,可领取python测试demo和学习资源,大家一起学python, 网罗天下方法,方便你我开发 。
希望可以帮助大家,如有问题可加QQ技术交流群: 668562416 如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议 如需转载请联系我,经过授权方可转载,谢谢
欢迎关注公众号「网罗开发」