爬取过程:

你好,李焕英 短评的URL:

https://movie.douban.com/subject/34841067/comments?start=20&limit=20&status=P&sort=new_score

在这里插入图片描述
分析要爬取的URL;
34841067:电影ID
start=20:开始页面
limit=20:每页评论条数

url = 'https://movie.douban.com/subject/%s/comments?start=%s&limit=20&sort=new_score&status=P % (movie_id, (i - 1) * 20)

在谷歌浏览器中按F12进入开发者调试模式,查看源代码,找到短评的代码位置,查看位于哪个div,哪个标签下:
在这里插入图片描述
可以看到评论在div[id=‘comments’]下的div[class=‘comment-item’]中的第一个span[class=‘short’]中,使用正则表达式提取短评内容,即代码为:

url = 'https://movie.douban.com/subject/%s/comments?start=%s&limit=20&sort=new_score&status=P' \
                  % (movie_id, (i - 1) * 20)
            req = requests.get(url, headers=headers)
            req.encoding = 'utf-8'
            comments = re.findall('<span class="short">(.*)</span>', req.text)

完整代码:

# 分析豆瓣你好李焕英的影评,生成词云
# https://movie.douban.com/subject/34841067/comments?start=20&limit=20&status=P&sort=new_score
# url = 'https://movie.douban.com/subject/%s/comments?start=%s&limit=20&sort=new_score&status=P '\
# % (movie_id, (i - 1) * 20)
import requests
from stylecloud import gen_stylecloud
import jieba
import re
from bs4 import BeautifulSoup
from wordcloud import STOPWORDS
headers = {
     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'
def jieba_cloud(file_name, icon):
    with open(file_name, 'r', encoding='utf8') as f:
        word_list = jieba.cut(f.read())
        result = " ".join(word_list)    # 分词用  隔开
        # 制作中文词云
        icon_name = " "
        if icon == "1":
            icon_name = ''
        elif icon == "2":
            icon_name = "fas fa-space-shuttle"
        elif icon == "3":
            icon_name = "fas fa-heartbeat"
        elif icon == "4":
            icon_name = "fas fa-bug"
        elif icon == "5":
            icon_name = "fas fa-thumbs-up"
        elif icon == "6":
            icon_name = "fab fa-qq"
        pic = str(icon) + '.png'
        if icon_name is not None and len(icon_name) > 0:
            gen_stylecloud(text=result,
                           size=1024,  # stylecloud 的大小(长度和宽度)
                           icon_name=icon_name,
                           font_path='simsun.ttc',
                           max_font_size=200,  # stylecloud 中的最大字号
                           max_words=2000,  # stylecloud 可包含的最大单词数
                           stopwords=True,  # 布尔值,用于筛除常见禁用词
                           custom_stopwords=STOPWORDS,
                           output_name=pic)
        else:
            gen_stylecloud(text=result, font_path='simsun.ttc', output_name=pic)
        return pic
def spider_comment(movie_id, page):
    comment_list = []
    with open("douban.txt", "a+", encoding='utf-8') as f:
        for i in range(1,page+1):
            url = 'https://movie.douban.com/subject/%s/comments?start=%s&limit=20&sort=new_score&status=P' \
                  % (movie_id, (i - 1) * 20)
            req = requests.get(url, headers=headers)
            req.encoding = 'utf-8'
            comments = re.findall('<span class="short">(.*)</span>', req.text)
            f.writelines('\n'.join(comments))
    print(comments)
# 主函数
if __name__ == '__main__':
    movie_id = '34841067'
    page = 10
    spider_comment(movie_id, page)
    jieba_cloud("douban.txt", "1")
    jieba_cloud("douban.txt", "2")
    jieba_cloud("douban.txt", "3")
    jieba_cloud("douban.txt", "4")
    jieba_cloud("douban.txt", "5")
    jieba_cloud("douban.txt", "6")

运行效果:

清洗词云:

可以看到炫酷的词云图了,然而仔细看的话,会发现出现了很多的”的”、“了”这样没有意义的字,很影响获取的印象,通过几个小时的折腾,终于让我找到了清洗词云的最简单的方法:
在这里加上:

        # 设置停用词
        stopwords_file = open('stopwords.txt', 'r', encoding='utf-8')
        stopwords = [words.strip() for words in stopwords_file.readlines()]

在这里插入图片描述
然后这里加上:

custom_stopwords=stopwords,  #定制停用词列表

在这里插入图片描述
这样就可以清洗掉无意义的词了!

停用词表参考:
Python文本分析之常用最全停用词表(stopwords)

stylecloud初识:

stylecloud是wordcloud优化改良版,操作简单,直接调用。

  • 可以使用 Font Awesome 提供的免费图标更改词云的形状;
  • 通过 palettable 更改调色板以自定义风格,更改背景颜色;
  • 添加梯度使颜色按照特定方向流动。

stylecloud参数详解:

text: 传入的字符串列表
file_path: 字符串的文本/ CSV的文件路径
gradient: 渐变方向 [「default:None]('horizontal')
size:stylecloud的大小(调大可提高图片清晰度)
icon_name: stylecloud形状的图标名称 [「default:」 fas fa-flag]
palette: 调色板 [「default:」 cartocolors.qualitative.Bold_5]
colors: 用作文本颜色的颜色 [「default:None]
background_color: 背景色(名称或十六进制)[「default:」 white]
max_font_size: stylecloud中的最大字体大小 [「default:200]
max_words: 要包含在stylecloud中的最大单词数 [「default:2000]
stopwords: 用于过滤掉常见的停用词 [「default:True]
custom_stopwords: list定制停用词列表 [「default:」 STOPWORDS, via word_cloud]
output_name: stylecloud的输出文件名 [「default:」 stylecloud.png]
font_path: 要在stylecloud中使用的字体的.ttf文件的路径 [「default:」 uses included Staatliches font]
random_state: 控制文字和颜色的随机状态 [「default:None]
collocations: 是否包括两个单词的搭配(二字组)。与基本word_cloud软件包的行为相同 [「default:True]
invert_mask: 是否反转图标掩码,因此单词填充除图标掩码以外的所有空格 [「default:False]
def gen_stylecloud(text=None,
                   file_path=None,   # 输入文本/CSV 的文件路径
                   size=512,  # stylecloud 的大小(长度和宽度)
                   icon_name='fas fa-flag',  # stylecloud 形状的图标名称(如 fas fa-grin)。[default: fas fa-flag]
                   palette='cartocolors.qualitative.Bold_5',  # 调色板(通过 palettable 实现)。[default: cartocolors.qualitative.Bold_6]
                   colors=None,
                   background_color="white",  # 背景颜色
                   max_font_size=200,  # stylecloud 中的最大字号
                   max_words=2000,  # stylecloud 可包含的最大单词数
                   stopwords=True,  # 布尔值,用于筛除常见禁用词
                   custom_stopwords=STOPWORDS,
                   icon_dir='.temp',
                   output_name='stylecloud.png',   # stylecloud 的输出文本名
                   gradient=None,  # 梯度方向
                   font_path=os.path.join(STATIC_PATH,
                                          'Staatliches-Regular.ttf'), # stylecloud 所用字体
                   random_state=None,  # 控制单词和颜色的随机状态
                   collocations=True,
                   invert_mask=False,
                   pro_icon_path=None,
                   pro_css_path=None):

词云形状:

使用 Font Awesome 提供的免费图标更改词云的形状。

https://fontawesome.dashgame.com/

https://fa5.dashgame.com/#/%E5%9B%BE%E6%A0%87

https://fontawesome.com/icons?d=gallery

对已的京东商品评论数据进行预处理、文本分频统计、展示。分别利用wordcloud库、pyecharts库的WordCloud和stylecloud库绘制,熟悉简单制作的方法。 可以让我们方便地识别出文本中的关键,其中单的大小代表它们的频率。有了这个,我们甚至在阅读之前就可以很好地了解文本的内容。 在本文中,我们将使用第三方 Pythonstylecloud,有了该库,可以通过简短的几行代码来制作漂亮的。本文来自技术群的小伙伴的分享,在此表示感谢。喜欢点赞、收藏、关注。 【注】文末可以加入我们的技术群 闲话少说,我们直接开始吧。 接下来,我们将用来制作的文本是偶像乔布斯在斯坦福大学演讲的一部分。文末链接可以获得对应的 .txt 文件,当然你也可以使 text=None, #输入字体 file_path=None, # 输入文本/CSV 的文件路径 gradient=None, #渐变方向(梯度方向),默认是horizontal size=512, 首先是豆瓣影评的短评,保存下来 豆瓣影评每页显示20条评论,我了前面50页的评论,先浏览找到翻页规律,批量生成网页url链接,然后解析每个页面,用的BeautifulSoup,提评论文字,保存为txt文件 然后将评论生成 代码里注释的很清晰了,就不赘述了 评论的代码 #引用time库、random库、requests库、BeautifulSoup4 import time import ran 注:虽然在短评首页显示《你好李焕英》共有41万多条短评,但是当浏览时,却发现只能查看前25页的短评,也就是说用户只能看到500条短评评论。发现这个问题后,查阅了一些相关资料,原来是 # 请求头 headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'} 1.获得豆瓣影评接口按浏览器F12调试,点开参数,查看url一栏:所有评论:url:https://movie.douban.com/subject/27119724/comments?status=P某一页的评论:url: https://movie.douban.com/subject/27119724/comments?start=40&limit=20&sort=new_score&sta... 过程: 你好李焕英 短评的URL: https://movie.douban.com/subject/34841067/comments?start=20&limit=20&status=P&sort=new_score 分析要的URL; 34841067:电影ID start=20:开始页面 limit=20:每页评论条数 url = 'https://movie.douban.com/subject/%s/comments?start=%s&limi 最近比较火的电影《你好李焕英》莫名戳中了大家的泪点,应用评论中的一句“妈妈永远比想象中的要爱我们” 虽然我没哭,但看大家都哭了,说明电影不在于多有深意,而是能引起大家共鸣的电影,才是好电影。 (完全瞎编的) 下面我们就来看一下《你好李焕英》在豆瓣影评中都有哪些优质的评论以及出现最多的是哪些。 1.确定数据所在的url https://movie.douban.com/subject/34841067/comments?percent_type=h&limit=20&status=P&a 本文将从何为虫、网页结构、python代码实现等方面逐步解析网络虫。1. 何为虫如今互联网上存储着大量的信息。作为普通网民,我们常常使用浏览器来访问互联网上的内容。但若是想要批量下载散布在互联网上的某一方面的信息(如某网站的所有片,某新闻网站的所有新闻,又或者豆瓣上所有电影的评分),人为的使用浏览器挨个打开网站搜查则过于费时费力。人为统计过于耗时耗力。因此,编写程序来自动抓互联网上我们想...