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 = []即可!
----------------------------------------------------------------------------------------------------------