V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jas0ndyq
V2EX  ›  问与答

求助,Python 查天气代码问题

  •  
  •   jas0ndyq · 2014-08-12 16:57:03 +08:00 · 2718 次点击
    这是一个创建于 3565 天前的主题,其中的信息可能已经有所发展或是发生改变。
    始终无法查到天气,返回“没有找到该城市”。好像是无法读取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 '没有找到该城市'
    2 条回复    2014-08-12 17:22:23 +08:00
    mengskysama
        1
    mengskysama  
       2014-08-12 17:14:00 +08:00
    而且你这个接口也失效了············
    mengskysama
        2
    mengskysama  
       2014-08-12 17:22:23 +08:00   ❤️ 1
    这个更好
    http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=B0ab8adf57f19eafa375e543812612b8
    直接用urlencode ocation查询就行了,这个key是个路人的.....自己去申请一个吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1128 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:57 · PVG 02:57 · LAX 11:57 · JFK 14:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.