后面会接触断点续传、aiohttp 、httpx (
线程池提交完任务(哪怕只开了一个 worker ),tqdm 显示进度条后按 Ctrl + C
并不会中断正在下载的任务。
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests
from tqdm import tqdm
def download(url):
try:
filesize = int(requests.get(url, stream=True).headers['Content-length']) or 0
filename = requests.get(url, stream=True).headers['content-disposition']
with requests.get(url, stream=True) as responese:
with tqdm(
total=filesize,
desc=f"Downloading: ",
unit="B",
unit_divisor=1024,
ascii=True,
unit_scale=True,
) as bar:
with open(filename, mode="ab") as f: # 追加模式
for chunk in responese.iter_content(1024):
f.write(chunk)
bar.update(len(chunk))
except (SystemExit, KeyboardInterrupt):
raise "KeyboardInterrupt"
urls = [...]
with ThreadPoolExecutor(maxworkers=8) as pool:
tasks = pool.map(download, urls)
for task in as_completed(tasks):
print(task.result())
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.