- 帖子
- 12
- 精华
- 0
- 积分
- 66
- 阅读权限
- 20
- 注册时间
- 2018-4-9
- 最后登录
- 2018-8-28
|
求助:使用查天气的程序,查询的天气不对是为什么?
# -*- coding: gbk -*-
import urllib2
import json
# web=urllib2.urlopen('http://www.baidu.com')
# content=web.read()
# f=open('test.html','w')
# f.write(content)
# 打印一个百度的首页;
from city import city
cityname=raw_input('你想查询哪个城市的天气?\n')
citycode=city.get(cityname)
if citycode:
try:
url=('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
content=urllib2.urlopen(url).read()
data=json.loads(content)
result=data['weatherinfo']
str_temp=('%s\n%s~%s')%(
result['weather'],
result['temp1'],
result['temp2']
)
print str_temp
except:
print'查询失败'
else:
print '没有找到该城市'
执行之后得到:
你想查询哪个城市的天气?上海
大雨转中雨
19℃~23℃
实际在中国天气网查到的天气是:晴,16℃-26℃,这是为什么呢?查询的数据来源不是中国天气网吗?
|
|