Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b32c7c

Browse files
添加文章
1 parent 3dcb836 commit 4b32c7c

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

‎day-121/douban-movie-top250.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import bs4 as bs4
2+
import requests
3+
import re
4+
from requests import RequestException
5+
6+
7+
'''
8+
1、爬取豆瓣 250 电影数据 【名称,导演,国家,链接。上映时间,类型,评分[五星,四星占比],评价人数】
9+
2、数据分析
10+
上映时间
11+
类型
12+
评分
13+
五星,四星,三星,二星,一星占比
14+
评价人数
15+
'''
16+
17+
18+
def get_page_html(url):
19+
headers = {
20+
'Referer': 'https://movie.douban.com/chart',
21+
'Host': 'movie.douban.com',
22+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
23+
}
24+
try:
25+
response = requests.get(url, headers=headers)
26+
if response.status_code == 200:
27+
return response.text
28+
return None
29+
except RequestException:
30+
return None
31+
32+
33+
def get_movie_url(html):
34+
ans = []
35+
soup = bs4.BeautifulSoup(html, 'html.parser')
36+
items = soup.select('li > div.item')
37+
for item in items:
38+
href = item.select('div.info > div.hd > a')[0]['href']
39+
ans.append(href)
40+
return ans
41+
42+
43+
# 【名称,链接。导演,国家,上映时间,类型,评分,[五星,四星,三星,二星,一星占比],评价人数】
44+
def get_movie_info(url):
45+
ans = {}
46+
html = get_page_html(url)
47+
soup = bs4.BeautifulSoup(html, 'html.parser')
48+
content = soup.find('div', id='content')
49+
50+
title = content.find('span', property='v:itemreviewed').text
51+
year = content.find('span', class_='year').text[1:5]
52+
directors = content.find('span', class_='attrs').find_all('a')
53+
director = []
54+
for i in range(len(directors)):
55+
director.append(directors[i].text)
56+
57+
country = content.find(text=re.compile('制片国家/地区')).next_element
58+
typeList = content.find_all('span', property='v:genre')
59+
type = []
60+
for object in typeList:
61+
type.append(object.text)
62+
63+
average = content.find('strong', property='v:average').text
64+
votes = content.find('span', property='v:votes').text
65+
66+
rating_per_items = content.find('div', class_='ratings-on-weight').find_all('div', class_='item')
67+
rating_per = [rating_per_items[0].find('span', class_='rating_per').text,
68+
rating_per_items[1].find('span', class_='rating_per').text]
69+
70+
return {'title': title, 'url': url, 'director': director, 'country': country, 'year': year, 'type': type,
71+
'average': average, 'votes': votes, 'rating_per': rating_per}
72+
73+
74+
def main():
75+
urls = []
76+
for i in range(1):
77+
start = i * 25
78+
url = 'https://movie.douban.com/top250?start=' + str(start) + '&filter='
79+
html = get_page_html(url)
80+
urls.append(get_movie_url(html))
81+
82+
index = 0;
83+
for url in urls:
84+
ans = get_movie_info(url)
85+
print(index, ans)
86+
index = index + 1
87+
88+
89+
def getUrls():
90+
url_init = 'https://movie.douban.com/top250?start={0}&filter='
91+
urls = [url_init.format(index * 25) for index in range(10)]
92+
return urls
93+
94+
def writeToFile(content):
95+
filename = 'doubanTop250.txt'
96+
with open(filename,'a') as f:
97+
f.write(content + '\n')
98+
99+
100+
if __name__ == '__main__':
101+
list_urls = getUrls()
102+
list_htmls = [get_page_html(url) for url in list_urls]
103+
movie_urls = [get_movie_url(html) for html in list_htmls]
104+
105+
movie_details = [get_movie_info(url) for url in movie_urls[0]]
106+
107+
for detail in movie_details:
108+
writeToFile(str(detail))
109+

‎day-121/doubanTop250.txt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{'title': '肖申克的救赎 The Shawshank Redemption', 'url': 'https://movie.douban.com/subject/1292052/', 'director': ['弗兰克·德拉邦特'], 'country': ' 美国', 'year': '1994', 'type': ['剧情', '犯罪'], 'average': '9.7', 'votes': '1786070', 'rating_per': ['84.9%', '13.6%']}
2+
{'title': '霸王别姬', 'url': 'https://movie.douban.com/subject/1291546/', 'director': ['陈凯歌'], 'country': ' 中国大陆 / 中国香港', 'year': '1993', 'type': ['剧情', '爱情', '同性'], 'average': '9.6', 'votes': '1317259', 'rating_per': ['81.6%', '16.0%']}
3+
{'title': '阿甘正传 Forrest Gump', 'url': 'https://movie.douban.com/subject/1292720/', 'director': ['罗伯特·泽米吉斯'], 'country': ' 美国', 'year': '1994', 'type': ['剧情', '爱情'], 'average': '9.5', 'votes': '1373104', 'rating_per': ['76.3%', '20.6%']}
4+
{'title': '这个杀手不太冷 Léon', 'url': 'https://movie.douban.com/subject/1295644/', 'director': ['吕克·贝松'], 'country': ' 法国', 'year': '1994', 'type': ['剧情', '动作', '犯罪'], 'average': '9.4', 'votes': '1567906', 'rating_per': ['74.1%', '22.5%']}
5+
{'title': '美丽人生 La vita è bella', 'url': 'https://movie.douban.com/subject/1292063/', 'director': ['罗伯托·贝尼尼'], 'country': ' 意大利', 'year': '1997', 'type': ['剧情', '喜剧', '爱情', '战争'], 'average': '9.5', 'votes': '878059', 'rating_per': ['79.9%', '17.6%']}
6+
{'title': '泰坦尼克号 Titanic', 'url': 'https://movie.douban.com/subject/1292722/', 'director': ['詹姆斯·卡梅隆'], 'country': ' 美国', 'year': '1997', 'type': ['剧情', '爱情', '灾难'], 'average': '9.4', 'votes': '1312109', 'rating_per': ['73.6%', '22.4%']}
7+
{'title': '千与千寻 千と千尋の神隠し', 'url': 'https://movie.douban.com/subject/1291561/', 'director': ['宫崎骏'], 'country': ' 日本', 'year': '2001', 'type': ['剧情', '动画', '奇幻'], 'average': '9.3', 'votes': '1404588', 'rating_per': ['71.7%', '24.3%']}
8+
{'title': "辛德勒的名单 Schindler's List", 'url': 'https://movie.douban.com/subject/1295124/', 'director': ['史蒂文·斯皮尔伯格'], 'country': ' 美国', 'year': '1993', 'type': ['剧情', '历史', '战争'], 'average': '9.5', 'votes': '705729', 'rating_per': ['78.3%', '19.1%']}
9+
{'title': '盗梦空间 Inception', 'url': 'https://movie.douban.com/subject/3541415/', 'director': ['克里斯托弗·诺兰'], 'country': ' 美国 / 英国', 'year': '2010', 'type': ['剧情', '科幻', '悬疑', '冒险'], 'average': '9.3', 'votes': '1334356', 'rating_per': ['70.6%', '25.2%']}
10+
{'title': "忠犬八公的故事 Hachi: A Dog's Tale", 'url': 'https://movie.douban.com/subject/3011091/', 'director': ['拉斯·霍尔斯道姆'], 'country': ' 美国 / 英国', 'year': '2009', 'type': ['剧情'], 'average': '9.3', 'votes': '907415', 'rating_per': ['72.5%', '22.8%']}
11+
{'title': "海上钢琴师 La leggenda del pianista sull'oceano", 'url': 'https://movie.douban.com/subject/1292001/', 'director': ['朱塞佩·托纳多雷'], 'country': ' 意大利', 'year': '1998', 'type': ['剧情', '音乐'], 'average': '9.3', 'votes': '1100271', 'rating_per': ['69.3%', '25.3%']}
12+
{'title': '机器人总动员 WALL·E', 'url': 'https://movie.douban.com/subject/2131459/', 'director': ['安德鲁·斯坦顿'], 'country': ' 美国', 'year': '2008', 'type': ['科幻', '动画', '冒险'], 'average': '9.3', 'votes': '881922', 'rating_per': ['71.3%', '24.3%']}
13+
{'title': '三傻大闹宝莱坞 3 Idiots', 'url': 'https://movie.douban.com/subject/3793023/', 'director': ['拉吉库马尔·希拉尼'], 'country': ' 印度', 'year': '2009', 'type': ['剧情', '喜剧', '爱情', '歌舞'], 'average': '9.2', 'votes': '1216603', 'rating_per': ['68.0%', '25.8%']}
14+
{'title': '楚门的世界 The Truman Show', 'url': 'https://movie.douban.com/subject/1292064/', 'director': ['彼得·威尔'], 'country': ' 美国', 'year': '1998', 'type': ['剧情', '科幻'], 'average': '9.3', 'votes': '960877', 'rating_per': ['68.0%', '27.5%']}
15+
{'title': '放牛班的春天 Les choristes', 'url': 'https://movie.douban.com/subject/1291549/', 'director': ['克里斯托夫·巴拉蒂'], 'country': ' 法国 / 瑞士 / 德国', 'year': '2004', 'type': ['剧情', '音乐'], 'average': '9.3', 'votes': '849122', 'rating_per': ['69.0%', '26.9%']}
16+
{'title': '星际穿越 Interstellar', 'url': 'https://movie.douban.com/subject/1889243/', 'director': ['克里斯托弗·诺兰'], 'country': ' 美国 / 英国 / 加拿大 / 冰岛', 'year': '2014', 'type': ['剧情', '科幻', '冒险'], 'average': '9.3', 'votes': '975690', 'rating_per': ['70.5%', '23.9%']}
17+
{'title': '大话西游之大圣娶亲 西遊記大結局之仙履奇緣', 'url': 'https://movie.douban.com/subject/1292213/', 'director': ['刘镇伟'], 'country': ' 中国香港 / 中国大陆', 'year': '1995', 'type': ['喜剧', '爱情', '奇幻', '古装'], 'average': '9.2', 'votes': '952981', 'rating_per': ['67.1%', '26.3%']}
18+
{'title': '熔炉 도가니', 'url': 'https://movie.douban.com/subject/5912992/', 'director': ['黄东赫'], 'country': ' 韩国', 'year': '2011', 'type': ['剧情'], 'average': '9.3', 'votes': '578013', 'rating_per': ['70.0%', '26.1%']}
19+
{'title': '龙猫 となりのトトロ', 'url': 'https://movie.douban.com/subject/1291560/', 'director': ['宫崎骏'], 'country': ' 日本', 'year': '1988', 'type': ['动画', '奇幻', '冒险'], 'average': '9.2', 'votes': '822559', 'rating_per': ['64.7%', '29.2%']}
20+
{'title': '无间道 無間道', 'url': 'https://movie.douban.com/subject/1307914/', 'director': ['刘伟强', '麦兆辉'], 'country': ' 中国香港', 'year': '2002', 'type': ['剧情', '悬疑', '犯罪'], 'average': '9.2', 'votes': '780367', 'rating_per': ['65.1%', '29.7%']}
21+
{'title': '教父 The Godfather', 'url': 'https://movie.douban.com/subject/1291841/', 'director': ['弗朗西斯·福特·科波拉'], 'country': ' 美国', 'year': '1972', 'type': ['剧情', '犯罪'], 'average': '9.3', 'votes': '603745', 'rating_per': ['70.3%', '24.1%']}
22+
{'title': '疯狂动物城 Zootopia', 'url': 'https://movie.douban.com/subject/25662329/', 'director': ['拜伦·霍华德', '瑞奇·摩尔', '杰拉德·布什'], 'country': ' 美国', 'year': '2016', 'type': ['喜剧', '动画', '冒险'], 'average': '9.2', 'votes': '1120945', 'rating_per': ['64.1%', '30.5%']}
23+
{'title': '当幸福来敲门 The Pursuit of Happyness', 'url': 'https://movie.douban.com/subject/1849031/', 'director': ['加布里埃莱·穆奇诺'], 'country': ' 美国', 'year': '2006', 'type': ['剧情', '家庭', '传记'], 'average': '9.1', 'votes': '980991', 'rating_per': ['61.9%', '31.4%']}
24+
{'title': '怦然心动 Flipped', 'url': 'https://movie.douban.com/subject/3319755/', 'director': ['罗伯·莱纳'], 'country': ' 美国', 'year': '2010', 'type': ['剧情', '喜剧', '爱情'], 'average': '9.0', 'votes': '1116908', 'rating_per': ['60.6%', '31.9%']}
25+
{'title': '触不可及 Intouchables', 'url': 'https://movie.douban.com/subject/6786002/', 'director': ['奥利维埃·纳卡什', '埃里克·托莱达诺'], 'country': ' 法国', 'year': '2011', 'type': ['剧情', '喜剧'], 'average': '9.2', 'votes': '641890', 'rating_per': ['66.7%', '28.5%']}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /