From f86cb65a17421aa9e469b9b2721408cb9b6230a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=A5=A0=E6=A5=A0?= <2509165032@student.example.com> Date: Tue, 23 Jun 2026 11:15:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/q1.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 data/q1.py diff --git a/data/q1.py b/data/q1.py new file mode 100644 index 0000000..74a7bf2 --- /dev/null +++ b/data/q1.py @@ -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) \ No newline at end of file