I am trying to parallelize requests to the Wikidata API using Python’s asyncio module.
My current synchronous script does the following:
import requests base_url = "https://www.wikidata.org/w/api.php&" payload = { "action": "query", "list": "search", "srsearch": search_term, "language": "en", "format": "json", "origin": "*", } res = requests.get(base_url, params=payload)
I am trying to do the same using asyncio
, to send requests asynchronously.
From this blogpost and the documentation, I understood that I need something like:
from aiohttp import ClientSession async with ClientSession() as session: async with session.get(url) as response: response = await response.read()
However, I did not manage to find how to add these payloads in the request. Do I have to reconstruct the URL manually or is there a way to send the payloads in asyncio?
Advertisement
Answer
Try the following code
async with ClientSession() as session: async with session.get(url, params=payload) as response: response = await response.read()