aiohttpモジュール

3486 ワード

requests.getは同期であり、非同期を実現するには非同期ネットワーク要求モジュールに基づいてurlを指定する要求送信=="aiohttp
pip3 install aiohttp 
 
import asyncio
import time
import aiohttp
headers = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36'
}
#      ClientSession        
urls = ['https://v3.bootcss.com/getting-started/','https://v3.bootcss.com/getting-started/','https://v3.bootcss.com/getting-started/']
async def get_page(url):
    async with aiohttp.ClientSession() as session:#  session          with  async  
        # post    async with await session.post(url) as response
        #u-a   async with await session.post(url,headers={xxxx}) as response
        #     get==> params   post ===> data,  ip:proxy = 'xxxx'(   )
        async with await session.get(url) as response: #      ,      await  
            #                 await    
            page_text = await response.text() #           read()     json() json
            print(page_text)
tasks = []
start = time.time()
for url in urls:
    c = get_page(url)
    task = asyncio.ensure_future(c)
    tasks.append(task)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print(end-start)