我使用 aiohttp 庫,我以 json 格式從服務器接收資料。在所有資料中,我只需要兩個需要顯示在串列中的欄位。但是當函式被呼叫時,協程被回傳。我如何取回串列?
async def request_geocoder_api(city):
url = f"https://nominatim.openstreetmap.org/search?city={city}&format=json"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response_json = await response.json
latitude = response_json[0]['lat']
longitude = response_json[0]['lon']
return [latitude, longitude]
函式呼叫:
answer_list = await request_geocoder_api(answer)
錯誤:
TypeError: object method can't be used in 'await' expression
uj5u.com熱心網友回復:
要獲得可等待的協程,請呼叫該json()函式。您錯過了()并且只是獲得了函式“指標”。
response_json = await response.json()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376800.html
