47 lines
1.3 KiB
Python
47 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"
|
||
}
|
||
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 |