Python 2.7 python天气API位置id
对于上面的命令,我得到10个结果。我想要一个特定的位置,比如印度的普纳。如何获取它?我查看了pywapi的源代码,发现搜索字符串将在Python 2.7 python天气API位置id,python-2.7,Python 2.7,对于上面的命令,我得到10个结果。我想要一个特定的位置,比如印度的普纳。如何获取它?我查看了pywapi的源代码,发现搜索字符串将在get\u loc\u id\u from\u waather\u com中被引用(url encode,例如,“,”将被引用到“%2C”) 因此,当您调用pywapi.get\u loc\u id\u from\u weather\u com(“Pune,MH,India”)时,它将请求url:http://xml.weather.com/search/searc
get\u loc\u id\u from\u waather\u com
中被引用(url encode,例如,“,”将被引用到“%2C”)
因此,当您调用pywapi.get\u loc\u id\u from\u weather\u com(“Pune,MH,India”)
时,它将请求url:http://xml.weather.com/search/search?where=Pune%2CMH%2CIndia
但不是http://wxdata.weather.com/wxdata/search/search?where=Pune,MH,印度
。而前者肯定没有结果
一个解决方案是您可以修改(破解)pywapi。只需编辑pywapi.py并找到get\u loc\u id\u from\u weather\u com
函数。将行url=LOCID\u SEARCH\u url%quote(SEARCH\u string)
替换为url=LOCID\u SEARCH\u url%quote(SEARCH\u string,,)
,现在您可以:
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}
附言: pywapi的源代码:
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}
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