当我尝试这段代码时:
import pandas as pd
import asyncio
import aiohttp
import time
# df = pd.read_json("./data/2023-10/fipe_codes_2023-10.json")
fipe_codes = [
"029162-5",
"074001-2",
"074002-0",
"074003-9",
"074004-7",
"061001-1"
]
data = []
url = "https://veiculos.fipe.org.br/api/veiculos/ConsultarAnoModeloPeloCodigoFipe"
def task_fabric(session):
tasks = []
# for index, row in df.head(10).iterrows():
for code in fipe_codes:
print(f"Working on fipe code: {code}")
payload = {
'codigoTipoVeiculo': '1',
'codigoTabelaReferencia': '302',
'codigoModelo': '',
'codigoMarca': '',
'ano': '',
'codigoTipoCombustivel': '',
'anoModelo': '',
'tipoVeiculo': '',
'modeloCodigoExterno': f'{code}',
}
tasks.append(session.post(url, data=payload, ssl=False))
return tasks
async def get_ano_modelo():
async with aiohttp.ClientSession() as session:
tasks = task_fabric(session)
responses = await asyncio.gather(*tasks)
for response in responses:
data.append(await response.json())
print(responses)
start_time = time.time() # Record the start time
asyncio.run(get_ano_modelo())
end_time = time.time() # Record the end time
elapsed_time = end_time - start_time # Calculate the elapsed time
出现此错误:
Working on fipe code: 029162-5 Working on fipe code: 074001-2 Working on fipe code: 074002-0 Working on fipe code: 074003-9 Working on fipe code: 074004-7 Working on fipe code: 061001-1 Traceback (most recent call last): File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/tests/asyncv2_requests.py", line 49, in \<module\> asyncio.run(get_ano_modelo()) File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete return future.result() File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/tests/asyncv2_requests.py", line 41, in get_ano_modelo responses = await asyncio.gather(\*tasks) File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1151, in send return self.\_coro.send(arg) File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/venv/lib/python3.10/site-packages/aiohttp/client.py", line 562, in \_request conn = await self.\_connector.connect( File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 540, in connect proto = await self.\_create_connection(req, traces, timeout) File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 901, in \_create_connection \_, proto = await self.\_create_direct_connection(req, traces, timeout) File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 1178, in \_create_direct_connection transp, proto = await self.\_wrap_create_connection( File "/home/guilherme/Documentos/vscode/projetos/emplacamentos/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 980, in \_wrap_create_connection return await self.\_loop.create_connection(\*args, \*\*kwargs) # type: ignore\[return-value\] # noqa File "/usr/lib/python3.10/asyncio/base_events.py", line 1060, in create_connection sock = await self.\_connect_sock( File "/usr/lib/python3.10/asyncio/base_events.py", line 969, in \_connect_sock await self.sock_connect(sock, address) File "/usr/lib/python3.10/asyncio/selector_events.py", line 501, in sock_connect return await fut RuntimeError: await wasn't used with future
我尝试了很多方法,但没有任何效果。谁能帮助我更好地理解这个错误?
在代码中我想获取所有json并将其放入数据列表中。
不要
await response.json()
。只需使用response.json()
。
对
await asyncio.gather(...)
的调用只有在一切完成后才会返回。它返回实际响应而不是等待对象。