Z
Z
zasara2022-01-17 17:39:11
Python
zasara, 2022-01-17 17:39:11

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())


The book_result array looks like this:
[
    {
        "author": null,
        "book": "102",
        "id": 507
    },
    {
        "author": null,
        "book": "12",
        "id": 412
    },
    {
        "author": null,
        "book": "17",
        "id": 490
    }
]


I need to convert it to be like this:
[
    {
        "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

1 answer(s)
C
cunning-seal, 2022-01-17
@cunning-seal

after receiving the data, add this data to the dictionary:
resp_json['author'] = t

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question