上传文件至 /
This commit is contained in:
34
q2_1.py
Normal file
34
q2_1.py
Normal file
@@ -0,0 +1,34 @@
|
||||
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://exam.detr.top/exam-b/movies"
|
||||
data = []
|
||||
resp = requests.get(url, headers=headers)
|
||||
resp.encoding = 'utf-8'
|
||||
html_content = resp.text
|
||||
with open("movies.html", "w", encoding="utf-8") as f:
|
||||
f.write(html_content)
|
||||
soup = bs(html_content, "html.parser")
|
||||
movies = []
|
||||
table = soup.find("table")
|
||||
rows = table.find_all("tr")[1:]
|
||||
for row in rows:
|
||||
cols = row.find_all("td")
|
||||
movie = {
|
||||
"id": int(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.append(movie)
|
||||
with open("movies.json", "w", encoding="utf-8") as f:
|
||||
json.dump({"data_id": 1, "movies": movies}, f, ensure_ascii=False, indent=4)
|
||||
Reference in New Issue
Block a user