在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  網(wǎng)絡(luò)安全/ 怎樣使用aiohttp請(qǐng)求多個(gè)url

怎樣使用aiohttp請(qǐng)求多個(gè)url

這是aiohttp官方請(qǐng)求單個(gè)url的方式,如果請(qǐng)求多個(gè)url并獲取內(nèi)容應(yīng)該怎么做?

import aiohttp
import asyncio

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, 'http://python.org')
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
回答
編輯回答
維他命
urls = ['http://baidu.com', 'http://qq.com']
async def get(url):
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, url)
        print(html)
tasks = [get(x) for x in urls]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
2018年6月14日 05:11