为 aiohttp 爬虫注入灵魂

为 aiohttp 爬虫注入灵魂

为 aiohttp 爬虫注入灵魂

摄影:产品经理
与产品经理在苏州的小生活
据说过异步爬虫的同窗,应该或多或少据说过aiohttp这个库。它经过 Python 自带的async/await实现了异步爬虫。api

使用 aiohttp,咱们能够经过 requests 的api写出并发量匹敌 Scrapy 的爬虫。网络

咱们在 aiohttp 的官方文档上面,能够看到它给出了一个代码示例,以下图所示:
为 aiohttp 爬虫注入灵魂session

咱们如今稍稍修改一下,来看看这样写爬虫,运行效率如何。并发

修改之后的代码以下:app

import asyncio
import aiohttp

template = 'http://exercise.kingname.info/exercise_middleware_ip/{page}'

async def get(session, page):
    url = template.format(page=page)
    resp = await session.get(url)
    print(await resp.text(encoding='utf-8'))

async def main():
    async with aiohttp.ClientSession() as session:
        for page in range(100):
            await get(session, page)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

这段代码访问个人爬虫练习站100次,获取100页的内容。异步

你们能够经过下面这个视频看看它的运行效率:
为 aiohttp 爬虫注入灵魂async

能够说,目前这个运行速度,跟 requests 写的单线程爬虫几乎没有区别,代码还多了那么多。ide

那么,应该如何正确释放 aiohttp 的超能力呢?oop

咱们如今把代码作一下修改:url

import asyncio
import aiohttp

template = 'http://exercise.kingname.info/exercise_middleware_ip/{page}'

async def get(session, queue):
    while True:
        try:
            page = queue.get_nowait()
        except asyncio.QueueEmpty:
            return
        url = template.format(page=page)
        resp = await session.get(url)
        print(await resp.text(encoding='utf-8'))

async def main():
    async with aiohttp.ClientSession() as session:
        queue = asyncio.Queue()
        for page in range(1000):
            queue.put_nowait(page)
        tasks = []
        for _ in range(100):
            task = get(session, queue)
            tasks.append(task)
        await asyncio.wait(tasks)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

在修改之后的代码里面,我让这个爬虫爬1000页的内容,咱们来看看下面这个视频。
为 aiohttp 爬虫注入灵魂

能够看到,目前这个速度已经能够跟 Scrapy 比一比了。而且你们须要知道,这个爬虫只有1个进程1个线程,它是经过异步的方式达到这个速度的。

那么,修改之后的代码,为何速度能快那么多呢?

关键的代码,就在:

tasks = []
for _ in range(100):
    task = get(session, queue)
    tasks.append(task)
await asyncio.wait(tasks)

在慢速版本里面,咱们只有1个协程在运行。而在如今这个快速版本里面,咱们建立了100个协程,并把它提交给asyncio.wait来统一调度。asyncio.wait会在全部协程所有结束的时候才返回。

咱们把1000个 URL 放在asyncio.Queue生成的一个异步队列里面,每个协程都经过 while True 不停从这个异步队列里面取 URL 并进行访问,直到异步队列为空,退出。

当程序运行时,Python 会自动调度这100个协程,当一个协程在等待网络 IO 返回时,切换到第二个协程并发起请求,在这个协程等待返回时,继续切换到第三个协程并发起请求……。程序充分利用了网络 IO 的等待时间,从而大大提升了运行速度。

最后,感谢实习生小河给出的这种加速方案。

为 aiohttp 爬虫注入灵魂

相关文章
相关标签/搜索