From 00bc332a2f28af7cb5bf9c3104a0f4f0869662ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E8=89=BA=E6=AC=A3?= <2509165020@student.example.com> Date: Sun, 5 Jul 2026 17:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20q2=5F1=5Fcrawler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- q2_1_crawler/电影爬取.txt | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 q2_1_crawler/电影爬取.txt diff --git a/q2_1_crawler/电影爬取.txt b/q2_1_crawler/电影爬取.txt new file mode 100644 index 0000000..8ae53fa --- /dev/null +++ b/q2_1_crawler/电影爬取.txt @@ -0,0 +1,36 @@ +import requests +import re +from bs4 import BeautifulSoup as bs +import json + +header = {'User-Agent':'Mozilla/5.0'} +url = 'https://exam.detr.top/exam-b/movies' + +html = requests.get(url, headers=header).text +# print(html) +open('movies.html','w',encoding='utf-8').write(html) +# print(html) +# print('============') +# fd = bs(html, 'html.parser').find('p', class_='meta') +fd = re.search(r'exam_fingerprint:\s*(\S+)',html).group(1) +# print(fd) + +resp = bs(html, 'html.parser').find_all('tr', class_='item-row') +# print(resp) +movies = [] +for i in resp: + c = [] + for r in i.find_all('td'): + c.append(r.text.strip()) + movies.append({ + 'id':int(c[0]), + 'title':c[1], + 'director':c[2], + 'year':int(c[3]), + 'rating':float(c[4]), + 'duration':int(c[5]), + 'genre':c[6], + 'actors_count':int(c[7]) + }) + +json.dump({"ID": fd, "movies":movies}, open('movies.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=2) \ No newline at end of file