Files
final-practice/20260609.2.py
2026-06-09 11:23:33 +08:00

44 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
from bs4 import BeautifulSoup
urls = [
"https://movie.douban.com/top250?start=0",
"https://movie.douban.com/top250?start=25"
]
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 = []
rank = 1
for url in urls:
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
items = soup.find_all("div", class_="item")
for item in items:
title = item.find("span", class_="title").text.strip()
actors_info = item.find("div", class_="bd").p.text.strip().split("\n")[0]
actors = actors_info.split("主演:")[-1].strip() if "主演:" in actors_info else ""
quote_tag = item.find("span", class_="inq")
quote = quote_tag.text.strip() if quote_tag else ""
movies.append({
"rank": rank,
"title": title,
"actors": actors,
"quote": quote
})
rank += 1
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(movies, f, ensure_ascii=False, indent=2)
print(f"成功爬取{len(movies)}部电影已保存为movies.json")