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

47 lines
1.3 KiB
Python
Raw Permalink 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
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"
}
url = "https://movie.douban.com/top250?start=0&filter="
data = []
resp = requests.get(url, headers=headers)
resp.encoding = 'utf-8'
soup = bs(resp.text, "html.parser")
# 遍历所有电影条目
items = soup.find_all("div", class_="item")
for i in range(len(items)):
# 提取电影标题
title = items[i].find("span", class_="title").get_text().strip()
# 提取主演actors异常赋值为无
try:
actors_text = items[i].find("div", class_="bd").get_text().strip()
actors = actors_text.split("主演: ")[1].split("\n")[0]
except:
actors = ""
# 提取经典台词quote异常赋值为无
try:
quote = items[i].find("div", class_="bd").find("p", class_="quote").get_text().strip()
except:
quote = ""
# 存入列表
data.append({
"title": title,
"actors": actors,
"quote": quote
})
print(data)
# 写入json文件
with open("movies.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
#pip install requests bs4
#python .\pachong.py