【Python中异步协程的基本框架】

import asyncio
import aiohttp
import aiofiles
import time
import os


async def download(url, path):
    # 文件夹不存在,则创建文件夹
    folder = os.path.exists(path)
    if not folder:
        os.makedirs(path)

    file_name = url.split('/')[-1]
    print('开始下载' + file_name)
    # 相当于requests
    async with aiohttp.ClientSession() as session:
        # 发送网络请求
        async with session.get(url) as resp:
            # 拿到服务器响应内容
            content = await resp.content.read()  # => resp.content
            # 将服务器响应内容写入文件
            file_path = os.path.join(path, file_name)
            async with aiofiles.open(file_path, mode="wb") as f:
                await f.write(content)
    print("下载完成-->" + file_name)


async def main():
    urls = [
        'https://p.qqan.com/up/2023-2/16770292779041273.jpg',
        'https://p.qqan.com/up/2023-2/16770292774260847.jpg',
        'https://p.qqan.com/up/2023-2/16770292777772764.jpg',
        'https://p.qqan.com/up/2023-2/16770292774114042.jpg',
        'https://p.qqan.com/up/2023-2/16770292778186811.jpg',
        'https://p.qqan.com/up/2023-2/16770292772698728.jpg',
        'https://p.qqan.com/up/2023-2/16770292778040007.jpg',
        'https://p.qqan.com/up/2023-2/16770292779869368.jpg',
        'https://p.qqan.com/up/2023-2/16770292774381284.jpg',
        'https://p.qqan.com/up/2023-2/16770292779722563.jpg',
        'https://p.qqan.com/up/2023-2/16770292774795332.jpg',
        'https://p.qqan.com/up/2023-2/16770292778307249.jpg',
        'https://p.qqan.com/up/2022-11/20221110938307688.jpg',
        'https://p.qqan.com/up/2022-10/20221021826286759.jpg',
        'https://p.qqan.com/up/2022-10/2022102091636377.jpg',
        'https://p.qqan.com/up/2022-10/2022101291126945.jpg',
        'https://p.qqan.com/up/2022-10/20221011840182297.jpg',
        'https://p.qqan.com/up/2022-9/2022928924122003.jpg',
        'https://p.qqan.com/up/2022-9/2022927850324414.jpg',
        'https://p.qqan.com/up/2022-9/2022923928104816.jpg',
        'https://p.qqan.com/up/2018-3/15215081458879225.jpg',
        'https://p.qqan.com/up/2022-12/20221215922316051.jpg',
        'https://p.qqan.com/up/2023-2/202322083544342.jpg',
        'https://p.qqan.com/up/2023-2/2023218928189046.jpg',
        'https://p.qqan.com/up/2023-2/2023217842261789.jpg',
        'https://p.qqan.com/up/2023-2/20232161023267004.jpg',
        'https://p.qqan.com/up/2023-2/2023216105335432.jpg',
        'https://p.qqan.com/up/2023-2/2023216953386713.jpg',
        'https://p.qqan.com/up/2023-2/20232229287405.jpg'
    ]
    tasks = []
    path = './download/'
    for url in urls:
        t = asyncio.create_task(download(url, path))
        tasks.append(t)
    await asyncio.wait(tasks)


if __name__ == '__main__':
    time_start = time.time()
    asyncio.run(main())
    time_end = time.time()
    print('总共耗时: ' + str(time_end - time_start) + '秒')

----------------------------------------------------------------------------------------------------------

真正搞懂此异步协程Demo,写其他爬虫直接套此框架即可

使用方法:将扫到的ruls替换到url_list = []即可!

----------------------------------------------------------------------------------------------------------

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值