python 天气预报获取

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

# -*- 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 天气预报获取_第1张图片

你可能感兴趣的:(python集,Python,技术汇总)