上传文件至 /
This commit is contained in:
47
26.03.31 43test4.py
Normal file
47
26.03.31 43test4.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import requests
|
||||
import os
|
||||
import json
|
||||
|
||||
# 模拟:从网页提取的海报URL(实际应从HTML中提取)
|
||||
poster_urls = [
|
||||
{'rank': 1, 'title': '肖申克的救赎', 'url': 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg'},
|
||||
{'rank': 2, 'title': '霸王别姬', 'url': 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2911205318.jpg'},
|
||||
{'rank': 3, 'title': '泰坦尼克号', 'url': 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p457760035.jpg'},
|
||||
]
|
||||
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
||||
}
|
||||
|
||||
# 创建目录
|
||||
os.makedirs('posters', exist_ok=True)
|
||||
|
||||
# 保存图片
|
||||
saved_info = []
|
||||
for info in poster_urls:
|
||||
try:
|
||||
# 发送请求获取图片
|
||||
response = requests.get(info['url'], headers=headers, timeout=10)
|
||||
image_data = response.content
|
||||
|
||||
# 保存图片
|
||||
filename = f"posters/{info['rank']}_{info['title']}.jpg"
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(image_data)
|
||||
|
||||
saved_info.append({
|
||||
'rank': info['rank'],
|
||||
'title': info['title'],
|
||||
'filename': filename,
|
||||
'size': len(image_data)
|
||||
})
|
||||
print(f'已保存: {filename} ({len(image_data)} bytes)')
|
||||
|
||||
except Exception as e:
|
||||
print(f'下载失败 {info["title"]}: {e}')
|
||||
|
||||
# 保存图片信息到JSON
|
||||
with open('posters/info.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(saved_info, f, ensure_ascii=False, indent=2)
|
||||
|
||||
print('\n图片信息已保存到 posters/info.json')
|
||||
Reference in New Issue
Block a user