import asyncio
import time
import aiohttp
start = time.time()
urls = [
'http://127.0.0.1',
'http://127.0.0.1',
'http://127.0.0.1',
]
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 session.get(url=url, headers=head) as response:
print('执行到3')
page_text = await response.text()
print(page_text[:15])
# 任务列表,存放多个任务对象
tasks = []
for url in urls:
tasks.append(get_page(url, headers))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
print('总耗时:', time.time()-start)
[Running] set PYTHONIOENCODING=utf8 && python "c:\Users\Administrator\Desktop\www\test.py"
执行到1
执行到2
执行到1
执行到2
执行到1
执行到2
执行到3
<!DOCTYPE html>
执行到3
<!DOCTYPE html>
执行到3
<!DOCTYPE html>
总耗时: 0.022365808486938477
[Done] exited with code=0 in 0.352 seconds
|