使用协程下载多幅图片(asyncio、aiohttp)
import asyncio
import aiohttp
import time
urls = ["https://www.umei.cc/d/file/20230906/9503a3c2752d24f896fc12706a54c9ab.jpg",
"https://www.umei.cc/d/file/20230906/50bec42cb5ca371a3ddc715c380ab91d.jpg",
"https://www.umei.cc/d/file/20230823/3aa0b5ac78d922916148db687cb17a66.jpg"]
async def aiodownload(url):
name = url.rsplit("/", 1)[1]
async with aiohttp.ClientSession() as session:
async with await session.get(url) as resp:
with open("img/" + name, mode="wb") as f:
f.write(await resp.content.read())
print(name, "已下载!")
async def main():
tasks = []
for url in urls:
tasks.append(asyncio.create_task(aiodownload(url)))
await asyncio.wait(tasks)
if __name__ == '__main__':
start = time.time()
asyncio.run(main())
end = time.time()
print("耗时:", end - start)
使用协程下载小说
import requests
import asyncio
import aiohttp
import json
"""
1.同步操作:访问getCatalog 拿到所有章节的cid和名称
2.异步操作:访问getchapterContent 下载所有的文章内容
"""
async def aiodownload(b_id, cid, title):
data = {"book_id": b_id,
"cid": f"{b_id}|{cid}",
"need_bookinfo": 1}
data = json.dumps(data)
url_chapter = f'https://dushu.baidu.com/api/pc/getChapterContent?data={data}'
async with aiohttp.ClientSession() as session:
async with await session.get(url_chapter) as resp:
dict = await resp.json()
with open('data_file/novel/' + title + '.csv', mode='w', encoding='utf-8') as f:
f.write(dict['data']['novel']['content'])
print(f"'{title}.csv'\t写入成功!")
async def getCatalog(url):
resp = requests.get(url)
dict = resp.json()
tasks = []
b_id = dict['data']['novel']['book_id']
for item in dict['data']['novel']['items']:
title = item['title']
cid = item['cid']
tasks.append(asyncio.create_task(aiodownload(b_id, cid, title)))
await asyncio.wait(tasks)
if __name__ == '__main__':
url_catalog = 'https://dushu.baidu.com/api/pc/getCatalog?data={%22book_id%22:%224306063500%22}'
asyncio.run(getCatalog(url_catalog))
print('所有章节下载完成!')