python 天气预报获取

来源:互联网 发布:gentoo linux下载 编辑:程序博客网 时间:2024/06/14 05:42

通过对地名的编码获取,通过编码获取到天气信息
源码如下:

# -*- encoding: utf-8 -*-import urllib2from cityCode import cityimport jsonlocation = 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']

运行结果截图:

weather.png

原创粉丝点击