上传文件至 /
This commit is contained in:
51
q1.py
Normal file
51
q1.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import requests
|
||||
import json
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
url = "https://exam.detr.top/exam-b/movies"
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
response.encoding = "utf-8"
|
||||
html_text = response.text
|
||||
|
||||
with open("movies.html", "w", encoding="utf-8") as f:
|
||||
f.write(html_text)
|
||||
|
||||
soup = BeautifulSoup(html_text, "html.parser")
|
||||
|
||||
movies_data = []
|
||||
data_id = ""
|
||||
|
||||
table = soup.find("table")
|
||||
if table:
|
||||
rows = table.find_all("tr")
|
||||
for row in rows[1:]:
|
||||
cols = row.find_all("td")
|
||||
if len(cols) >= 8:
|
||||
movie = {
|
||||
"id": cols[0].text.strip(),
|
||||
"title": cols[1].text.strip(),
|
||||
"director": cols[2].text.strip(),
|
||||
"year": int(cols[3].text.strip()),
|
||||
"rating": float(cols[4].text.strip()),
|
||||
"duration": int(cols[5].text.strip()),
|
||||
"genre": cols[6].text.strip(),
|
||||
"actors_count": int(cols[7].text.strip())
|
||||
}
|
||||
movies_data.append(movie)
|
||||
|
||||
data_id_tag = soup.find("span", class_="data-id")
|
||||
if data_id_tag:
|
||||
data_id = data_id_tag.text.strip()
|
||||
|
||||
result = {
|
||||
"data_id": data_id,
|
||||
"movies": movies_data
|
||||
}
|
||||
|
||||
with open("movies.json", "w", encoding="utf-8") as f:
|
||||
json.dump(result, f, ensure_ascii=False, indent=4)
|
||||
Reference in New Issue
Block a user