44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
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") |