Python爬取豆瓣的影评并生产词云

本文介绍了一种使用Python爬虫从豆瓣网站抓取图书评论的方法,并利用jieba分词和WordCloud库生成词云,展示了数据抓取到可视化分析的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 爬取评论的代码如下所示

from  urllib import request
from bs4  import BeautifulSoup  as bs
import os

i= 1
while(i<6):
    i+=1
    url = 'https://book.douban.com/subject/26904658/comments/hot?p='+str(i)
content = request.urlopen(url)
    
html = content.read().decode('utf-8')
soup = bs(html,'html.parser')

comment_div_lists = soup.find_all('span',class_='short')
#print(comment_div_lists)
file = open('pinglun2.txt','w')  #该条命令是如果文件存在就打开,如果不存在会自动创建一个txt文件
for comment in comment_div_lists:
    print(comment.text)
    
    file.write(comment.text)

file.close()



    

生成词云的代码如下

import wordcloud
import jieba
from scipy.misc import imread

mask = imread("china.jpg")

txt = open("pinglun2.txt","r",encoding="utf-8")
t = txt.read()
txt.close()
ls = jieba.lcut(t)
print(len(ls))
tt = " ".join(ls)

w = wordcloud.WordCloud(font_path="simsun.ttc" ,mask =mask,width=1000,height=1000,background_color="white")
w.generate(tt)
w.to_file("pinglun2.jpg")
print("成功")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Test9912

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值