Files
final-practice/pachong.py
2026-06-11 16:35:01 +08:00

46 lines
1.3 KiB
Python

import requests
from bs4 import BeautifulSoup as bs
import json
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
}
data = []
for start in range(0, 250, 25):
url = f"https://movie.douban.com/top250?start={start}&filter="
resp = requests.get(url, headers=headers)
resp.encoding = "utf-8"
soup = bs(resp.text, "html.parser")
items = soup.find_all("div", class_="item")
for item in items:
title = item.find("span", class_="title").get_text()
info_text = item.find("div", class_="bd").get_text().strip()
info_lines = [line.strip() for line in info_text.split("\n") if line.strip()]
actors = ""
for line in info_lines:
if "主演:" in line:
actors = line.split("主演:")[-1].strip()
break
quote = ""
quote_tag = item.find("p", class_="quote")
if quote_tag:
quote = quote_tag.get_text().strip()
data.append({
"title": title,
"actors": actors,
"quote": quote
})
print(data)
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)