TCPConnector 限制连接数.py 928 字节
Newer Older
梦想橡皮擦's avatar
梦想橡皮擦 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import time

import asyncio
import aiohttp
from bs4 import BeautifulSoup


async def get_title(session, url):
    async with session.get(url) as res:
        print("正在采集:", url)
        html = await res.text()
        soup = BeautifulSoup(html, 'html.parser')
        title_tags = soup.find_all(attrs={'class': 'item-title'})
        event_names = [item.a.text for item in title_tags]
        print(event_names)



async def main():
    connector = aiohttp.TCPConnector(limit=1)  # 限制同时连接数
    async with aiohttp.ClientSession(connector=connector) as session:
        tasks = [asyncio.ensure_future(get_title(session, "http://www.lishiju.net/hotevents/p{}".format(i))) for i in
                 range(111)]
        await asyncio.wait(tasks)



if __name__ == '__main__':
    start_time = time.perf_counter()
    asyncio.run(main())
    print("代码运行时间为:", time.perf_counter() - start_time)