删除 26062309.py
This commit is contained in:
50
26062309.py
50
26062309.py
@@ -1,50 +0,0 @@
|
|||||||
import requests
|
|
||||||
import json
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
|
|
||||||
# 请求检测头(题目硬性要求)
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
url = "https://exam.detr.top/exam-b/movies"
|
|
||||||
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
response.encoding = "utf-8"
|
|
||||||
|
|
||||||
# 保存网页源码
|
|
||||||
with open("movies.html", "w", encoding="utf-8") as f:
|
|
||||||
f.write(response.text)
|
|
||||||
|
|
||||||
# 解析表格数据
|
|
||||||
soup = BeautifulSoup(response.text, "html.parser")
|
|
||||||
table = soup.find("table")
|
|
||||||
tr_rows = table.find_all("tr")[1:]
|
|
||||||
|
|
||||||
movie_list = []
|
|
||||||
for row in tr_rows:
|
|
||||||
cell = row.find_all("td")
|
|
||||||
info = {
|
|
||||||
"id": int(cell[0].text.strip()),
|
|
||||||
"title": cell[1].text.strip(),
|
|
||||||
"director": cell[2].text.strip(),
|
|
||||||
"year": int(cell[3].text.strip()),
|
|
||||||
"rating": float(cell[4].text.strip()),
|
|
||||||
"duration": int(cell[5].text.strip()),
|
|
||||||
"genre": cell[6].text.strip(),
|
|
||||||
"actors_count": int(cell[7].text.strip())
|
|
||||||
}
|
|
||||||
movie_list.append(info)
|
|
||||||
|
|
||||||
# 提取页面数据编号
|
|
||||||
data_code = soup.find("code").get_text(strip=True)
|
|
||||||
result = {
|
|
||||||
"data_id": data_code,
|
|
||||||
"movies": movie_list
|
|
||||||
}
|
|
||||||
|
|
||||||
# 写入JSON文件
|
|
||||||
with open("movies.json", "w", encoding="utf-8") as f:
|
|
||||||
json.dump(result, f, ensure_ascii=False, indent=4)
|
|
||||||
|
|
||||||
print("✅ 爬取完成,两个文件已正常生成")
|
|
||||||
Reference in New Issue
Block a user