import re
from PIL import Image
from io import BytesIO
import os
# 输入话题链接
topic_url = input("请输入话题链接:")
# 提取__biz和album_id参数
biz_match = re.search(r'__biz=(.*?)&', topic_url)
biz = biz_match.group(1) if biz_match is not None else None
album_match = re.search(r'album_id=(.*?)&', topic_url)
album_id = album_match.group(1) if album_match is not None else None
# 构造请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
# 发送请求获取网页源代码
response = requests.get(topic_url, headers=headers)
# 使用正则表达式匹配出图片链接和标题
img_links = re.findall('data-src="(.*?)"', response.text)
titles = re.findall('data-title="(.*?)"', response.text)
# 遍历标题和图片链接,下载图片并保存
print("正在下载图片...")
for i, img_link in enumerate(img_links):
try:
response = requests.get(img_link)
if response.status_code == 200:
img = Image.open(BytesIO(response.content))
ext = img.format.lower()
if ext in ["png", "gif", "jpg", "jpeg"]:
file_path = os.path.join(os.path.expanduser("~"), "Desktop/TH", f"{i+1}.{ext}")
with open(file_path, "wb") as f:
f.write(response.content)
print(f"{i+1}.jpg 图片下载完成!")
else:
print("不支持的图片格式")
else:
print(f"请求失败,状态码:{response.status_code}")
except Exception as e:
print(f"下载第{i+1}张图片时出错:{e}")
continue
print("全部图片下载完成!")
这段代码默认将下载的图片保存在桌面的TH文件夹内。如需修改保存路径,请修改 os.path.join(os.path.expanduser("~"), "Desktop/TH", f"{i+1}.{ext}") 这一行的路径。之前看到的帖子都没有对图片类型以及报错进行处理,故我额外增加了一些代码,保证能正常运行下去