使用pywapi从用户输入获取位置ID

时间:2017-10-20 09:26:42

标签: python-3.x python-unicode weather-api

我正在尝试制作一个简单的CLI应用来检索天气数据。不幸的是,我还没有达到这个目标,因为我已经陷入了困境。到目前为止,这是我的代码:

import  pywapi, string

loc=input("What is the city you're closest to?")
loc=loc.lower

#this will give you a dictionary of all cities in the world with this city's name Be specific (city, country)!
loc_id=pywapi.get_location_ids(loc)
#apparently this is a needed workaround to access last item of dictionary
for i in loc_id:
    loc_id=i

#before I go on to code anything further, I just want to use print to check that I've got the two variables I need
print (loc,loc_id)

当被问到一个城市时,我可以进入伦敦,例如英国伦敦,但两者都会出错:(这是在我的本地机器上)

Traceback (most recent call last):
  File "get_weather.py", line 7, in <module>
    loc_id=pywapi.get_location_ids(loc)
  File "/home/james/.local/lib/python3.4/site-packages/pywapi.py", line 825, in get_location_ids
    loc_id_data = get_loc_id_from_weather_com(search_string)
  File "/home/james/.local/lib/python3.4/site-packages/pywapi.py", line 856, in get_loc_id_from_weather_com
    url = LOCID_SEARCH_URL % quote(search_string)
  File "/usr/lib/python3.4/urllib/parse.py", line 694, in quote
    return quote_from_bytes(string, safe)
  File "/usr/lib/python3.4/urllib/parse.py", line 719, in quote_from_bytes
    raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes

这个错误,不同的是,当我使用Pythonanywhere时

Traceback (most recent call last):
  File "/home/pydavith/get_weather.py", line 7, in <module>
    loc_id=pywapi.get_location_ids(loc)
  File "/home/pydavith/.local/lib/python3.6/site-packages/pywapi.py", line 825, in get_location_ids
    loc_id_data = get_loc_id_from_weather_com(search_string)
  File "/home/pydavith/.local/lib/python3.6/site-packages/pywapi.py", line 852, in get_loc_id_from_weather_co
m
    search_string = unidecode(search_string)
  File "/usr/local/lib/python3.6/dist-packages/unidecode/__init__.py", line 48, in unidecode_expect_ascii
    bytestring = string.encode('ASCII')
AttributeError: 'builtin_function_or_method' object has no attribute 'encode'

有人知道这里出了什么问题吗?我已经大肆宣传和谷歌广告,但没有任何事情发生。帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

好的,我自己也找到了可行的答案。我在这里分享代码,以防万一这有助于其他人。

我在这里改变了很多东西,很难知道从哪里开始。也就是说,我最初发布的代码中存在许多错误和需要的改进。

user != nil

无论如何,这是我想要的,所以我希望这对某些初学者有用。如果有人有任何进一步的改进,请告诉我。