zoukankan      html  css  js  c++  java
  • 单线程+多任务+异步协程爬虫模块

    import requests
    import aiohttp
    import time
    import asyncio
    s = time.time()
    urls = [
    'http://127.0.0.1:5000/bobo',
    'http://127.0.0.1:5000/jay'
    ]

    # async def get_request(url):
    # page_text = requests.get(url).text
    # return page_text
    async def get_request(url):
    async with aiohttp.ClientSession() as s:
    async with await s.get(url=url) as response:
    page_text = await response.text()
    print(page_text)
    return page_text
    tasks = []
    for url in urls:
    c = get_request(url)
    task = asyncio.ensure_future(c)
    tasks.append(task)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))

    print(time.time()-s)
  • 相关阅读:
    3.04
    3.03
    3.02
    3.01
    2.27
    2.25小账本6
    2.24小账本5
    2.23小账本4
    2.22小账本3
    git常用命令
  • 原文地址:https://www.cnblogs.com/well-666/p/12853408.html
Copyright © 2011-2022 走看看