协程爬虫小demo

要使用协程,需要用到asyncio异步函数库 以及httpx异步请求库 (必须条件)
import asyncio

import httpx


async def test(url):
    print('Get: %s' % url)
    async with httpx.AsyncClient() as client: # 如果需要使用异步请求一定要用到httpx这个异步请求库协程才会生效,不然会一直阻塞,==协程失效
        response =await client.get(url)
        content = response.text
        print('%d bytes received from %s.' % (len(content), url))


async def ff():
    print(1)


task_list = []
for x in ['http://httpbin.org/ip', 'http://httpbin.org/uuid', 'http://httpbin.org/user-agent']:
    task_list.append(asyncio.ensure_future(test(x)))
loop = asyncio.get_event_loop()
# task=asyncio.ensure_future(ff())
loop.run_until_complete(asyncio.wait(task_list))

# 一套操作下来之后,整体性能开销比多线程小多了,多线程就更不用说了,开销小了,速度反而更快了
协程nb,不过需要注意一个坑的地方,就是你用协程请求一定需要异步网络请求库,不然还是会网络阻塞变成单进程请求
demo很简单,通俗易懂,新手入门协程必看
搜索