更新 260326 2509165026.py
This commit is contained in:
@@ -1,26 +1,42 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
import time
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
movies = []
|
||||
for page in range(2):
|
||||
url = f"https://movie.douban.com/top250?start={page*25}"
|
||||
response = requests.get(url, headers=headers)
|
||||
soup = BeautifulSoup(response.text, "html.parser")
|
||||
|
||||
url = f'https://picsum.photos/'
|
||||
items = soup.find_all("div", class_="item")
|
||||
for idx, item in enumerate(items):
|
||||
|
||||
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'}
|
||||
rank = page * 25 + idx + 1
|
||||
|
||||
response = requests.get(url, headers=headers,timeout=10)
|
||||
response.encoding = 'utf-8'
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
title = item.find("span", class_="title").text.strip()
|
||||
|
||||
img_tags = soup.select('img.resize')
|
||||
for i, img_tag in enumerate(img_tags[:5]):
|
||||
img_src = img_tag.get('src')
|
||||
print(f"正在下载第 {i+1} 张图片:{img_src}")
|
||||
img_respnse = requests.get(img_src,headers=headers, timeout=10)
|
||||
info_text = item.find("div", class_="bd").p.text.strip()
|
||||
actors_line = info_text.split("\n")[0] if "\n" in info_text else info_text
|
||||
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 "暂无短评"
|
||||
|
||||
img_response = requests.get(img_src, timeout=10)
|
||||
movies.append({
|
||||
"rank": rank,
|
||||
"title": title,
|
||||
"actors": actors,
|
||||
"short_comment": short_comment
|
||||
})
|
||||
|
||||
with open('image.jpg', 'wb') as f:
|
||||
f.write(img_response.content)
|
||||
print("下载完成")
|
||||
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")
|
||||
Reference in New Issue
Block a user