协程中请求url过慢问题

我在爬虫中使用了协程,但是运行起来还是同步操作,没有达到异步效果,请问是哪里出了问题了,还请指正一下代码
不胜感激

import requests
from lxml import etree
import aiohttp
import asyncio
import aiofiles
import time

# 提取第二页URL
async def index_url(number):
    img_urls = []
    async with aiohttp.ClientSession() as session:
        async with await session.get(f'https://wallpaperscraft.com/catalog/anime/page{number}') as resp:
            tree = etree.HTML(await resp.text()).xpath('/html/body/div/div[2]/div[2]/div/div[2]/div[1]/ul/li//a/@href')
            for t in tree:
                url = 'https://wallpaperscraft.com' + t
                img_urls.append(url)
    return img_urls

# 请求第二页URL
async def to_imgurl(url):
    async with aiohttp.ClientSession() as session:
        async with await session.get(url) as resp:
            src = etree.HTML(await resp.text()).xpath('/html/body/div/div[2]/div[2]/div/div[2]/div[1]/div[1]/img/@src')
            print(src)


if __name__ == '__main__':
    for i in range(1, 4):
        list_url = asyncio.run(index_url(i))
        for l in list_url:
            asyncio.run(to_imgurl(l))

该回答引用GPTᴼᴾᴱᴺᴬᴵ
你的代码实现了协程,但是却是在同步的方式下调用协程,导致没有实现异步效果。
-
在你的代码中,for循环依次遍历每一页的URL,然后再依次遍历每个URL中的图片链接,每次遍历都会调用asyncio.run()函数,此函数会启动一个事件循环来执行协程,并且会阻塞当前线程,直到事件循环结束。也就是说,当前循环的执行是同步的,每个URL中的协程只有前一个协程结束后才会开始执行。
-
如果想要实现异步效果,可以考虑将整个循环改为异步方式,每个URL中的协程使用asyncio.gather()函数包装起来,一次性启动并发执行,例如:

import requests
from lxml import etree
import aiohttp
import asyncio
import aiofiles
import time
 
# 提取第二页URL
async def index_url(number):
    img_urls = []
    async with aiohttp.ClientSession() as session:
        async with await session.get(f'https://wallpaperscraft.com/catalog/anime/page{number}') as resp:
            tree = etree.HTML(await resp.text()).xpath('/html/body/div/div[2]/div[2]/div/div[2]/div[1]/ul/li//a/@href')
            for t in tree:
                url = 'https://wallpaperscraft.com' + t
                img_urls.append(url)
    return img_urls
 
# 请求第二页URL
async def to_imgurl(url):
    async with aiohttp.ClientSession() as session:
        async with await session.get(url) as resp:
            src = etree.HTML(await resp.text()).xpath('/html/body/div/div[2]/div[2]/div/div[2]/div[1]/div[1]/img/@src')
            print(src)
 
async def main():
    tasks = []
    for i in range(1, 4):
        list_url = await index_url(i)
        for l in list_url:
            task = asyncio.create_task(to_imgurl(l))
            tasks.append(task)
    await asyncio.gather(*tasks)
 
if __name__ == '__main__':
    asyncio.run(main())

在上述代码中,main()函数使用asyncio.create_task()函数创建协程任务,并使用asyncio.gather()函数启动协程任务的并发执行,以实现异步效果。此外,asyncio.run()函数也改为了asyncio.run(main())的方式来启动事件循环,以便正确处理异步操作。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^