import asyncio #协程处理模块
import time
import aiohttp #异步请求页面模块
start = time.time()
urls=[
'www.sogou.com',
'www.baidu.com',
'www.goubanjia.com',
]
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
}
async def get_page(url,head):
print('执行到1')
async with aiohttp.ClientSession() as session:
print('执行到2')
async with await session.get(url=url,headers=head) as response:
print('执行到3')
page_text = await response.text()
print(page_text)
#任务列表,存放多个任务对象
tasks = []
for url in urls:
c = get_page(url,headers)
task = asyncio.ensure_future(c)
tasks.append(task)
loop = asyncio.get_event_loop()
#固定格式:将任务列表封装到wait方法中
loop.run_until_complete(asyncio.wait(tasks))
print('总耗时:',time.time()-start)
|