异步协程
2018-11-24 本文已影响0人
一言不合就跑步
import asyncio
import aiohttp
async def get(url):
headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
session = aiohttp.ClientSession()
response = await session.get(url,headers=headers)
result = await response.text()
await session.close()
return result
async def request(url):
result = await get(url)
tasks = []
for i in range(page, page + 19):
url =
task_signal = asyncio.ensure_future(request(url))
tasks.append(task_signal)
print(tasks)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))