python Weather API位置ID

时间:2013-12-24 14:00:09

标签: python-2.7

a=pywapi.get_loc_id_from_weather_com("pune")


{0: (u'TTXX0257', u'Pune, OE, Timor-leste'), 
 1: (u'INXX0102', u'Pune, MH, India'), 
 2: (u'BRPA0444', u'Pune, PA, Brazil'),
 3: (u'FRBR2203', u'Punel, 29, France'), 
 4: (u'IDVV9705', u'Punen, JT, Indonesia'),
 5: (u'IRGA2787', u'Punel, 19, Iran'),
 6: (u'IRGA2788', u'Punes, 19, Iran'),
 7: (u'IDYY7030', u'Punen, JI, Indonesia'), 
 8: (u'RSUD1221', u'Punem, UD, Russia'),
 9: (u'BUXX2256', u'Punevo, 09, Bulgaria'),    
'count': 10}

对于上面的命令,我得到10个结果。我想要一个特定的位置,如浦那,MH,印度。我怎么得到它?

1 个答案:

答案 0 :(得分:1)

我查看了pywapi的源代码,发现在get_loc_id_from_waather_com中会引用searchstring(url encode,例如','将引用“%2C”)。

因此,当您致电pywapi.get_loc_id_from_weather_com(" Pune,MH,India")时,它会请求网址:http://xml.weather.com/search/search?where=Pune%2CMH%2CIndia,但不会http://wxdata.weather.com/wxdata/search/search?where=Pune,MH,India。而且形成者肯定没有结果。

解决方案是您可以修改(破解)pywapi。只需编辑pywapi.py并找到get_loc_id_from_weather_com函数。将行url = LOCID_SEARCH_URL % quote(search_string)替换为url = LOCID_SEARCH_URL % quote(search_string, ',')现在您可以:

In [2]: import pywapi

In [3]: pywapi.get_loc_id_from_weather_com("Pune,MH,India") # no spaces
Out[3]: {0: (u'INXX0102', u'Pune, MH, India'), 'count': 1}




PS: pywapi的源代码:

def get_loc_id_from_weather_com(search_string):
    """Get location IDs for place names matching a specified string.
    Same as get_location_ids() but different return format.

    Parameters:
      search_string: Plaintext string to match to available place names.
      For example, a search for 'Los Angeles' will return matches for the
      city of that name in California, Chile, Cuba, Nicaragua, etc as well
      as 'East Los Angeles, CA', 'Lake Los Angeles, CA', etc.

    Returns:
      loc_id_data: A dictionary of tuples in the following format:
      {'count': 2, 0: (LOCID1, Placename1), 1: (LOCID2, Placename2)}

    """
    # Weather.com stores place names as ascii-only, so convert if possible
    try: 
        # search_string = unidecode(search_string.encode('utf-8'))
        search_string = unidecode(search_string)
    except NameError:
        pass 

    url = LOCID_SEARCH_URL % quote(search_string) 
    # change to:url = LOCID_SEARCH_URL % quote(search_string, ',') 
    try: 
        handler = urlopen(url)
    except URLError:
        return {'error': 'Could not connect to server'}
    if sys.version > '3': 
        # Python 3
        content_type = dict(handler.getheaders())['Content-Type']
    else:
        # Python 2
        content_type = handler.info().dict['content-type']
    try: 
        charset = re.search('charset\=(.*)', content_type).group(1)
    except AttributeError:
        charset = 'utf-8'
    if charset.lower() != 'utf-8':
        xml_response = handler.read().decode(charset).encode('utf-8')
    else:
        xml_response = handler.read()
    dom = minidom.parseString(xml_response)
    handler.close()

    loc_id_data = {} 
    try: 
        num_locs = 0
        for loc in dom.getElementsByTagName('search')[0].getElementsByTagName('loc'):
            loc_id = loc.getAttribute('id')  # loc id
            place_name = loc.firstChild.data  # place name
            loc_id_data[num_locs] = (loc_id, place_name)
            num_locs += 1 
        loc_id_data['count'] = num_locs
    except IndexError:
        error_data = {'error': 'No matching Location IDs found'}
        return error_data
    finally:
        dom.unlink()

    return loc_id_data