Auxiliary 2023-02-22 18:10 采纳率: 37.5%
浏览 33
已结题

协程中请求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))

  • 写回答

2条回答 默认 最新

  • MarkHan_ 2023-02-22 19:45
    关注

    该回答引用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())的方式来启动事件循环,以便正确处理异步操作。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 修改了问题 2月22日
  • 创建了问题 2月22日

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效