爬取豆瓣top250的电影信息以及几个评价,然后用jieba库分词看一下情感极性把top250的电影分词几个情感类,分别写入Angry、Disgusted、Happy、Neutral、Sad、Scared、Surpriser的xlsx文件中,完整可用,带注释
时间: 2024-06-09 07:10:42 浏览: 16
首先,我们需要安装所需的库,包括 requests、beautifulsoup4、jieba 和 openpyxl。可以使用以下命令进行安装:
```
pip install requests beautifulsoup4 jieba openpyxl
```
接下来,我们使用 requests 库获取豆瓣电影 Top250 的页面信息,并使用 beautifulsoup4 库解析页面:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/top250'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
```
然后,我们需要找到所有电影的链接,以便获取每个电影的详细信息和评价。在豆瓣电影 Top250 页面中,每个电影都有一个 class 为 "hd" 的 div 标签,其中包含了电影的链接。我们可以使用以下代码来获取所有电影链接:
```python
movie_links = []
for movie in soup.find_all('div', class_='hd'):
movie_link = movie.a['href']
movie_links.append(movie_link)
```
接下来,我们需要访问每个电影的详细页面,并从页面中获取电影信息和评价。在豆瓣电影页面中,电影信息和评价都包含在 class 为 "subject" 的 div 标签中。我们可以使用以下代码来获取电影信息和评价:
```python
import re
movie_info = []
movie_comments = []
for link in movie_links:
response = requests.get(link)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取电影信息
info = soup.find('div', class_='subject')
title = info.h1.span.text
year = re.search(r'\d+', info.find('span', class_='year').text).group()
director = info.find('a', rel='v:directedBy').text
actors = [a.text for a in info.find_all('a', rel='v:starring')]
genres = [a.text for a in info.find_all('span', property='v:genre')]
rating = info.find('strong', class_='rating_num').text
summary = info.find('span', property='v:summary').text.strip()
movie_info.append([title, year, director, actors, genres, rating, summary])
# 获取电影评价
comments = soup.find('div', class_='comment-list hot')
for comment in comments.find_all('p'):
movie_comments.append(comment.text.strip())
```
现在,我们已经获取了所有电影的信息和评价。接下来,我们需要使用 jieba 库对评价进行分词,并使用情感词典来判断情感类别。我们可以使用以下代码来实现:
```python
import jieba
import jieba.analyse
import os
# 加载情感词典
jieba.load_userdict(os.path.join(os.path.dirname(__file__), 'sentiment_dict.txt'))
# 定义情感词典标签
sentiment_tags = {
'angry': ['愤怒', '生气'],
'disgusted': ['厌恶', '恶心'],
'happy': ['开心', '高兴'],
'neutral': ['中性'],
'sad': ['悲伤', '难过'],
'scared': ['害怕', '恐惧'],
'surprised': ['惊讶', '吃惊']
}
# 分词并打上情感标签
movie_sentiments = []
for comment in movie_comments:
words = jieba.lcut(comment)
sentiment = 'neutral'
for word in words:
for tag, sentiment_words in sentiment_tags.items():
if word in sentiment_words:
sentiment = tag
break
if sentiment != 'neutral':
break
movie_sentiments.append([comment, sentiment])
```
最后,我们需要将电影按照情感类别写入不同的 Excel 文件中。我们可以使用 openpyxl 库来实现:
```python
from openpyxl import Workbook
# 创建 Excel 文件
workbook = Workbook()
for tag in sentiment_tags:
workbook.create_sheet(title=tag)
# 写入数据
for movie, sentiment in zip(movie_info, movie_sentiments):
comment, tag = sentiment
sheet = workbook[tag]
sheet.append(movie + [comment])
# 保存文件
workbook.save('top250_sentiments.xlsx')
```
完整代码如下:
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)