普通网络请求,基于协程网络请求和基于feture的协程网络请求

普通网络请求

import requests
url_list = [
            'https://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
            'https://www2.autoimg.cn/newsdfs/g30/M01/3C/E2/120x90_0_autohomecar__ChcCSV2BBICAUntfAADjJFd6800429.jpg',
            'https://www3.autoimg.cn/newsdfs/g26/M0B/3C/65/120x90_0_autohomecar__ChcCP12BFCmAIO83AAGq7vK0sGY193.jpg'
        ]
def get_img(url):
    response=requests.get(url).content
    file_name=url.rsplit('_')[-1]
    with open(file_name,'wb') as f:
        f.write(response)

for url in url_list:
    get_img(url)

基于协程的网路请求

import asyncio
import aiohttp

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36'
}

url_list = [
    'https://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
    'https://www2.autoimg.cn/newsdfs/g30/M01/3C/E2/120x90_0_autohomecar__ChcCSV2BBICAUntfAADjJFd6800429.jpg',
    'https://www3.autoimg.cn/newsdfs/g26/M0B/3C/65/120x90_0_autohomecar__ChcCP12BFCmAIO83AAGq7vK0sGY193.jpg'
]


async def download_img(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            content = await response.content.read()
            file_name = url.rsplit('_')[-1]
            with open(file_name, mode='wb') as f:
                f.write(content)




async def main():
    # task = [download_img(url) for url in url_list]
    # await asyncio.gather(*task)
    task=[asyncio.create_task(download_img(url)) for url in url_list]
    await asyncio.wait(task)


if __name__ == "__main__":
    # asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(main())

基于feture的网络请求

import requests
import asyncio


async def download_img(url):
    loop = asyncio.get_running_loop()
    feture = loop.run_in_executor(None, requests.get, url)
    response=await feture
    file_name=url.rsplit('_')[-1]
    with open(file_name,mode='wb') as f:
        f.write(response.content)

if __name__ == "__main__":
    # asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    url_list = [
        'https://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
        'https://www2.autoimg.cn/newsdfs/g30/M01/3C/E2/120x90_0_autohomecar__ChcCSV2BBICAUntfAADjJFd6800429.jpg',
        'https://www3.autoimg.cn/newsdfs/g26/M0B/3C/65/120x90_0_autohomecar__ChcCP12BFCmAIO83AAGq7vK0sGY193.jpg'
    ]
    task= [download_img(url) for url in url_list]
    asyncio.run(asyncio.wait(task))

 

posted @ 2023-05-31 16:13  壮九  阅读(27)  评论(0)    收藏  举报