Answer the question
In order to leave comments, you need to log in
How to change the value of a key in an array with asynchronous parsing?
I parse a site with books through asinka, but the result is without authors. Therefore, I separately wrote out the authors and make requests through the authors list of authors. I need to change the value of the author key to the authors themselves, I don’t understand how to do this.
Here is the code:
book_result = []
async def x_get(session, t: str):
url = f'...={t}'
async with session.get(url, ssl=False) as resp:
resp_json = await resp.json()
book_result.append(resp_json)
async def x_scraper():
authors = ['IDEN', 'WILL', 'LONDON']
async with aiohttp.ClientSession() as session:
tasks = []
for t in authors:
task = asyncio.create_task(x_get(session, t))
tasks.append(task)
await asyncio.gather(*tasks)
asyncio.run(x_scraper())
[
{
"author": null,
"book": "102",
"id": 507
},
{
"author": null,
"book": "12",
"id": 412
},
{
"author": null,
"book": "17",
"id": 490
}
]
[
{
"author": WILL,
"book": "102",
"id": 507
},
{
"author": IDEN,
"book": "12",
"id": 412
},
{
"author": LONDON,
"book": "17",
"id": 490
}
]
Answer the question
In order to leave comments, you need to log in
after receiving the data, add this data to the dictionary:
resp_json['author'] = t
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question