爬虫之豆瓣图书评论词云

from urllib import request
from bs4 import BeautifulSoup as bs
import re
import codecs
import jieba  #分词包
import numpy    #numpy计算包
import pandas as pd #分词用到
import matplotlib.pyplot as plt  #绘图包
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0,5.0)
from wordcloud import WordCloud #词云包

#爬取豆瓣最受关注图书榜
resp = request.urlopen('https://book.douban.com/chart?subcat=I')
html_data = resp.read().decode('utf-8')

#转化为BeautifulSoup对象
soup = bs(html_data,'html.parser')

#搜索最受关注的图书列表
topchart_book =soup.find_all('ul',class_='chart-dashed-list')

#搜索列表中所有图书
topchart_book_list = topchart_book[0].find_all('li',class_='media clearfix')

#新建数组用于存放后续的数据
topchart_list = []

#遍历图书馆列表,从中过滤出我们所需的信息
for item in topchart_book_list:
    #新建字典用于存放我们的图书信息,之后可用class来存储
    topchart_dict = {}

    #搜索到具体信息的位置
    book_item = item.find('a',class_='fleft')

    #得到图书ID
    topchart_dict['id'] = book_item['href'].split('/')[4]   

    #得到图书名称
    topchart_dict['name'] = book_item.getText().replace('\t','').replace('\n','').replace(' ','')  #图书名字
    
    #将图书信息加入到数组中
    topchart_list.append(topchart_dict)
# print(topchart_list)

#拼接出图书对应的详情页
requrl = 'https://book.douban.com/subject/'+topchart_list[0]['id']+'/comments/hot'+'?'+'p-1'

#爬取热门第一页中的评论信息
resp = request.urlopen(requrl)
html_data = resp.read().decode('utf-8')
soup = bs(html_data,'html.parser')

#搜索到评论所在div
comment_div_lits = soup.find_all('div',class_='comment')

#新建数组用于存放评论信息
eachCommentList = []

for item in comment_div_lits:
    if item.find_all('p')[0].string is not None:
        eachCommentList.append(item.find_all('p')[0].string)
# print(eachCommentList)

#为了方便进行数据进行清洗,我们将列表中的数据放在一个字符串数组中
commits = ''
for k in range(len(eachCommentList)):
    commits = commits + (str(eachCommentList[k])).strip()

# print(commits)
# exit()

#可以看到所有的评论已经变成一个字符串了,但我们发现评论中还有不少标点符号等
#这些符号对我们进行词频统计根本就没有用,因此将他们清除,所用的方法是正则表达式
pattern = re.compile(r'[\u4e00-\u9fa5]+')
filterdata = re.findall(pattern,commits)
cleaned_comments = ''.join(filterdata)
# print('评论数组')
# print(eachCommentList)
# print('评论字符串')
# print(commits)
# print('去除标点符号的评论')
# print(cleaned_comments)
# exit()

#在这里使用的是结巴分词,如果没有安装结巴分词,可以在控制台用 pip install jieba安装
segment = jieba.lcut(cleaned_comments)
# print(segment)
# exit()
#可以使用pandas库将分词转化成dataframe格式,head()方法只查看前五项内容
words_df = pd.DataFrame({'word':segment})
# print(words_df)
# print(words_df.head())
# exit()

#可以看到我们的数据中有“我”、“很”等虚词(停用词)
#而这些词在任何场景中都是高频时,并且没有实际含义,所以我们要对他们进行清除
#把停用词放在一个stopwords.txt文件中,将我们的数据与停用词进行对比即可

stopwords=pd.read_csv("stopwords.txt",index_col=False,quoting=3,sep="\t",names=['stopword'],encoding='gbk')
# print(stopwords)
# exit()
words_df=words_df[~words_df.word.isin(stopwords.stopword)]
# print(words_df.head())
# exit()

#接下来进行词频统计
words_stat=words_df.groupby(by=['word'])['word'].agg(['count'])
# print(words_stat.head())

#对统计结果进行排序
words_stat=words_stat.reset_index().sort_values(by=["count"],ascending=False)
# print(words_stat.head())

#词云展示
wordcloud = WordCloud(font_path="simhei.ttf",background_color="white",max_font_size=80)    #指定字体类型、字体大小、背景颜色
word_frequence = {x[0]:x[1] for x in words_stat.head(1000).values}
# print(word_frequence)
wordcloud = wordcloud.fit_words(word_frequence)
# print(wordcloud)
# exit()

#matplotlib包对图片进行绘制,绘制成功后,返回一个AxesImage对象,要在窗口上显示这个对象,我们可以用show()函数进行
#在某些练习环境下可以省略show()函数,也能自动显示出来
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

 

转载于:https://www.cnblogs.com/lsm-boke/p/9940326.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值