49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
import os
|
|
import re
|
|
import requests
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
from tqdm import tqdm
|
|
|
|
# 创建packet文件夹
|
|
os.makedirs("packet", exist_ok=True)
|
|
|
|
# 读取link.txt并提取所有https链接
|
|
with open("link.txt", "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
# 匹配所有https链接
|
|
links = re.findall(r"https://[^\s,]+", content)
|
|
|
|
# 下载函数
|
|
def download_file(url):
|
|
local_filename = os.path.join("packet", url.split("/")[-1].split("?")[0])
|
|
try:
|
|
with requests.get(url, stream=True) as r:
|
|
r.raise_for_status()
|
|
total_size = int(r.headers.get("content-length", 0))
|
|
with open(local_filename, "wb") as f, tqdm(
|
|
desc=os.path.basename(local_filename),
|
|
total=total_size,
|
|
unit="B",
|
|
unit_scale=True,
|
|
unit_divisor=1024,
|
|
leave=False
|
|
) as bar:
|
|
for chunk in r.iter_content(chunk_size=8192):
|
|
if chunk:
|
|
f.write(chunk)
|
|
bar.update(len(chunk))
|
|
return local_filename
|
|
except Exception as e:
|
|
print(f"下载失败: {url} -> {e}")
|
|
return None
|
|
|
|
# 多线程下载
|
|
with ThreadPoolExecutor(max_workers=8) as executor:
|
|
future_to_url = {executor.submit(download_file, url): url for url in links}
|
|
for future in as_completed(future_to_url):
|
|
url = future_to_url[future]
|
|
result = future.result()
|
|
if result:
|
|
print(f"已下载: {result}")
|