分享

强大的异步爬虫 with aiohttp

 heii2 2018-06-14


是谁的小眼睛还没有看老师这里?



你们最爱的小歪老师最近又有了一个大大大发现,说起爬虫,现在网络上大多讲的是requests、scarpy,却没有说起过这样一个神器:aiohttp。


究竟它是什么呢?


官网上是这样介绍的:Async HTTP client/server for asyncio and Python,翻译过来就是asyncio和Python的异步HTTP客户端/服务器。


它的主要特点是酱紫的:

◆ 1.支持客户端和HTTP服务器;

◆ 2.无需使用Callback Hell即可支持Server WebSockets和Client WebSockets;

◆ 3.Web服务器具有中间件,信号和可插拔路由。


话不多说,看老师操作!!!



Client example:

import aiohttp
import asyncio

async def fetch(session, url):
   async with session.get(url) as response:
       return await response.text()

async def main():
   async with aiohttp.ClientSession() as session:
       html = await fetch(session, 'http:///headers')
       print(html)

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



{'headers':{'Accept':'*/*','Accept-Encoding':'gzip, deflate','Connection':'close','Host':'','User-Agent':'Python/3.6 aiohttp/3.2.1'}}



from aiohttp import web

async def handle(request):
   name = request.match_info.get('name', 'Anonymous')
   text = 'Hello, ' + name
   return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
               web.get('/{name}', handle)])

web.run_app(app)



======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)


aiohttp与requests


去翻一下官方文档Client Quickstart,会让人感觉特别熟悉,很多用法和requests相似。


async with aiohttp.ClientSession() as session:
   async with session.get('http:///get') as resp:
       print(resp.status)
       print(await resp.text())

首先,官方推荐使用ClientSession来管理会话,这不就是requests中的session吗?用法也类似,使用session.get()去发送get请求,返回的resp中就有我们所需要的数据了,用法也和requests一样,text()文本,.json()直接打印返回的json数据,headers什么的也一样,更多内容参考官方文档Response object。


既然已经有了requests了,那为什么还要说aiohttp了?重点来了,aiohttp是异步的。在python3.5中,加入了asyncio/await关键字,使得回调的写法更加直观和人性化。而aiohttp是一个提供异步web服务的库,asyncio可以实现单线程并发IO操作。


requests写爬虫是同步的,是等待网页下载好才会执行下面的解析、入库操作,如果在下载网页时间太长会导致阻塞,使用multiprocessing或者threading加速爬虫也是一种方法。


我们现在使用的aiohttp是异步的,简单来说,就是不需要等待,你尽管去下载网页就好了,我不用傻傻的等待你完成才进行下一步,我还有别的活要干。这样就极大的提高了下载网页的效率。


另外,Scrapy也是异步的,是基于Twisted事件驱动的。在任何情况下,都不要写阻塞的代码。阻塞的代码包括:

◆ 1.访问文件、数据库或者Web;

◆ 2.产生新的进程并需要处理新进程的输出,如运行shell命令;

◆ 3.执行系统层次操作的代码,如等待系统队列。


代码示例


需要注意的是,你需要时刻在你的代码中使用异步操作,你如果在代码中使用同步操作,爬虫并不会报错,但是速度可能会受影响。


import asyncio

import aiohttp
from bs4 import BeautifulSoup

import logging

class AsnycGrab(object):

   def __init__(self, url_list, max_threads):

       self.urls = url_list
       self.results = {}
       self.max_threads = max_threads

   def __parse_results(self, url, html):

       try:
           soup = BeautifulSoup(html, 'html.parser')
           title = soup.find('title').get_text()
       except Exception as e:
           raise e

       if title:
           self.results[url] = title

   async def get_body(self, url):
       async with aiohttp.ClientSession() as session:
           async with session.get(url, timeout=30) as response:
               assert response.status == 200
               html = await response.read()
               return response.url, html

   async def get_results(self, url):
       url, html = await self.get_body(url)
       self.__parse_results(url, html)
       return 'Completed'

   async def handle_tasks(self, task_id, work_queue):
       while not work_queue.empty():
           current_url = await work_queue.get()
           try:
               task_status = await self.get_results(current_url)
           except Exception as e:
               logging.exception('Error for {}'.format(current_url), exc_info=True)

   def eventloop(self):
       q = asyncio.Queue()
       [q.put_nowait(url) for url in self.urls]
       loop = asyncio.get_event_loop()
       tasks = [self.handle_tasks(task_id, q, ) for task_id in range(self.max_threads)]
       loop.run_until_complete(asyncio.wait(tasks))
       loop.close()


if __name__ == '__main__':
   async_example = AsnycGrab(['http://',
              'https://www.',
              'https://github.com/',
              'https://zhangslob./',
              'https://www.zhihu.com/'], 5)
   async_example.eventloop()
   print(async_example.results)


其他异步库


因为爬虫不仅仅只有下载这块,还会有操作数据库,这里提供两个异步库:aioredis、motor


import asyncio
import aioredis

loop = asyncio.get_event_loop()

async def go():
   conn = await aioredis.create_connection(
       'redis://localhost', loop=loop)
   await conn.execute('set', 'my-key', 'value')
   val = await conn.execute('get', 'my-key')
   print(val)
   conn.close()
   await conn.wait_closed()
loop.run_until_complete(go())
# will print 'value'

文档:aioredis


import motor.motor_asyncio

client = motor.motor_asyncio.AsyncIOMotorClient('mongodb://localhost:27017')

db = client['test_database']
collection = db['test_collection']

async def do_insert():
   document = {'key': 'value'}
   result = await db.test_collection.insert_one(document)
   print('result %s' % repr(result.inserted_id))

async def do_find_one():
   document = await db.test_collection.find_one({'i': {'$lt': 1}})
   pprint.pprint(document)

文档:motor


嗯……本文仅仅介绍了aiohttp作为Client的用法,有兴趣的同学可以去研究下作为Server的用法,同样很强大。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多