diff --git a/q2_1_crawler/movies.html b/q2_1_crawler/movies.html new file mode 100644 index 0000000..9a8e5b7 --- /dev/null +++ b/q2_1_crawler/movies.html @@ -0,0 +1,152 @@ + + + + + + + + 电影列表 + + + +

电影列表

+

数据编号:B-20260623-2074

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号电影名导演上映年份评分时长(分钟)类型主演数
1放牛班的春天Frank Darabont20198.2134爱情2
2忠犬八公的故事陈凯歌20106.7130爱情4
3星际穿越Robert Zemeckis20107.7109动画2
4阿甘正传James Cameron20059.298爱情4
5肖申克的救赎宫崎骏20228.9111冒险4
6霸王别姬Christopher Nolan20059.4169悬疑5
7千与千寻Lasse Hallström20009.2106冒险4
8盗梦空间Rajkumar Hirani20148.0101悬疑2
9泰坦尼克号Christophe Barratier19969.293喜剧5
10三傻大闹宝莱坞Christopher Nolan20037.9114冒险4
+ + \ No newline at end of file diff --git a/q2_1_crawler/movies.json b/q2_1_crawler/movies.json new file mode 100644 index 0000000..75f763f --- /dev/null +++ b/q2_1_crawler/movies.json @@ -0,0 +1,102 @@ +[ + { + "id": "1", + "title": "放牛班的春天", + "director": "Frank Darabont", + "year": 2019, + "rating": 8.2, + "duration": 134, + "genre": "爱情", + "actors_count": 2 + }, + { + "id": "2", + "title": "忠犬八公的故事", + "director": "陈凯歌", + "year": 2010, + "rating": 6.7, + "duration": 130, + "genre": "爱情", + "actors_count": 4 + }, + { + "id": "3", + "title": "星际穿越", + "director": "Robert Zemeckis", + "year": 2010, + "rating": 7.7, + "duration": 109, + "genre": "动画", + "actors_count": 2 + }, + { + "id": "4", + "title": "阿甘正传", + "director": "James Cameron", + "year": 2005, + "rating": 9.2, + "duration": 98, + "genre": "爱情", + "actors_count": 4 + }, + { + "id": "5", + "title": "肖申克的救赎", + "director": "宫崎骏", + "year": 2022, + "rating": 8.9, + "duration": 111, + "genre": "冒险", + "actors_count": 4 + }, + { + "id": "6", + "title": "霸王别姬", + "director": "Christopher Nolan", + "year": 2005, + "rating": 9.4, + "duration": 169, + "genre": "悬疑", + "actors_count": 5 + }, + { + "id": "7", + "title": "千与千寻", + "director": "Lasse Hallström", + "year": 2000, + "rating": 9.2, + "duration": 106, + "genre": "冒险", + "actors_count": 4 + }, + { + "id": "8", + "title": "盗梦空间", + "director": "Rajkumar Hirani", + "year": 2014, + "rating": 8.0, + "duration": 101, + "genre": "悬疑", + "actors_count": 2 + }, + { + "id": "9", + "title": "泰坦尼克号", + "director": "Christophe Barratier", + "year": 1996, + "rating": 9.2, + "duration": 93, + "genre": "喜剧", + "actors_count": 5 + }, + { + "id": "10", + "title": "三傻大闹宝莱坞", + "director": "Christopher Nolan", + "year": 2003, + "rating": 7.9, + "duration": 114, + "genre": "冒险", + "actors_count": 4 + } +] \ No newline at end of file diff --git a/q2_1_crawler/q2_1.py b/q2_1_crawler/q2_1.py new file mode 100644 index 0000000..03aea6b --- /dev/null +++ b/q2_1_crawler/q2_1.py @@ -0,0 +1,31 @@ +import re +import requests +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 diff --git a/q2_1_crawler/q2_2.py b/q2_1_crawler/q2_2.py new file mode 100644 index 0000000..97ec1c0 --- /dev/null +++ b/q2_1_crawler/q2_2.py @@ -0,0 +1,37 @@ +import json + +with open('q2_1_crawler\movies.json', 'r', encoding='utf-8') as f: + movies = json.load(f) + +if movies: + sorted_by_rating = sorted(movies, key=lambda x: x['rating']) + lowest_movie = sorted_by_rating[0] + highest_movie = sorted_by_rating[-1] + + print("2.1 最高评分电影:") + print(f"电影名: {highest_movie['title']}, 评分: {highest_movie['rating']}") + print("\n最低评分电影:") + print(f"电影名: {lowest_movie['title']}, 评分: {lowest_movie['rating']}") + +genre_counts = {} +for movie in movies: + genre = movie['genre'] + genre_counts[genre] = genre_counts.get(genre, 0) + 1 + +print("\n2.2 各类型电影数量字典:") +print(genre_counts) + +director_counts = {} +for movie in movies: + director = movie['director'] + director_counts[director] = director_counts.get(director, 0) + 1 + +print("\n2.3 各导演电影数量字典:") +print(director_counts) + +count_2020_later = 0 +for movie in movies: + if movie['year'] >= 2020: + count_2020_later += 1 + +print(f"\n2.4 2020年(含)以后上映的电影数量: {count_2020_later} 部") \ No newline at end of file