求助,Python 查天气代码问题

2014-08-12 16:57:03 +08:00
 jas0ndyq
始终无法查到天气,返回“没有找到该城市”。好像是无法读取city中的json信息的原因,printcitycode返回“None”。。。求解啊,代码如下
# -*- coding: utf-8 -*-
import urllib2
import json
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 '没有找到该城市'
2732 次点击
所在节点    问与答
2 条回复
mengskysama
2014-08-12 17:14:00 +08:00
而且你这个接口也失效了············
mengskysama
2014-08-12 17:22:23 +08:00
这个更好
http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=B0ab8adf57f19eafa375e543812612b8
直接用urlencode ocation查询就行了,这个key是个路人的.....自己去申请一个吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/127458

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX