上传文件至 /
This commit is contained in:
42
260618 2505155046.py
Normal file
42
260618 2505155046.py
Normal file
@@ -0,0 +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")
|
||||||
|
|
||||||
|
items = soup.find_all("div", class_="item")
|
||||||
|
for idx, item in enumerate(items):
|
||||||
|
|
||||||
|
rank = page * 25 + idx + 1
|
||||||
|
|
||||||
|
title = item.find("span", class_="title").text.strip()
|
||||||
|
|
||||||
|
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 "暂无短评"
|
||||||
|
|
||||||
|
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")
|
||||||
Reference in New Issue
Block a user