41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import requests
|
|
import os
|
|
import json
|
|
|
|
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}')
|
|
|
|
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') |