更新 260326 2509165026.py

This commit is contained in:
2026-06-18 16:03:01 +08:00
parent 73898feb4f
commit a5ceb6013f

View File

@@ -1,26 +1,42 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import json
import time
url = f'https://picsum.photos/' headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'} }
response = requests.get(url, headers=headers,timeout=10) movies = []
response.encoding = 'utf-8' for page in range(2):
soup = BeautifulSoup(response.text, 'html.parser') url = f"https://movie.douban.com/top250?start={page*25}"
response = requests.get(url, headers=headers)
img_tags = soup.select('img.resize') soup = BeautifulSoup(response.text, "html.parser")
for i, img_tag in enumerate(img_tags[:5]):
img_src = img_tag.get('src') items = soup.find_all("div", class_="item")
print(f"正在下载第 {i+1} 张图片:{img_src}") for idx, item in enumerate(items):
img_respnse = requests.get(img_src,headers=headers, timeout=10)
rank = page * 25 + idx + 1
img_response = requests.get(img_src, timeout=10) title = item.find("span", class_="title").text.strip()
with open('image.jpg', 'wb') as f: info_text = item.find("div", class_="bd").p.text.strip()
f.write(img_response.content) actors_line = info_text.split("\n")[0] if "\n" in info_text else info_text
print("下载完成") actors = actors_line.split("主演: ")[-1].strip() if "主演: " in actors_line else "未知主演"
short_comment_tag = item.find("span", class_="inq")
short_comment = short_comment_tag.text.strip() if short_comment_tag else "暂无短评"
movies.append({
"rank": rank,
"title": title,
"actors": actors,
"short_comment": short_comment
})
time.sleep(1)
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(movies, f, ensure_ascii=False, indent=4)
print("✅ 数据采集完成,已保存到 movies.json")