Day5《青春有你2》评论数据爬取与词云分析

参考思路

step1:爱奇艺《青春有你2》评论数据爬取(参考链接:https://www.iqiyi.com/v_19ryfkiv8w.html#curid=15068699100_9f9bab7e0d1e30c494622af777f4ba39)

  • 爬取任意一期正片视频下评论
  • 评论条数不少于1000条

step2:词频统计并可视化展示

  • 数据预处理:清理清洗评论中特殊字符(如:@#¥%、emoji表情符),清洗后结果存储为txt文档
  • 中文分词:添加新增词(如:青你、奥利给、冲鸭),去除停用词(如:哦、因此、不然、也好、但是)
  • 统计top10高频词
  • 可视化展示高频词

step3:绘制词云

  • 根据词频生成词云
  • 可选项-添加背景图片,根据背景图片轮廓生成词云

step4:结合PaddleHub,对评论进行内容审核

实现环境:linux环境下的 AI Studio 平台(百度飞桨)

网页分析

随便打开一期视频分析,点击“更多评论”这里,没有可以获取的相关链接。在这里插入图片描述

因此通过Network 对行为进行检测,选择JS,在点击更多评论同时,捕获到get_comments.actionXXXX, 在右边Response 可以观察到内容,评论的内容就包括在里面
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实践

1. 配置与准备

中文分词需要jieba
词云绘制需要wordcloud
可视化展示中需要的中文字体
网上公开资源中找一个中文停用词表
根据分词结果自己制作新增词表
准备一张词云背景图(附加项,不做要求,可用hub抠图实现)
paddlehub配置
 #飞桨线上平台
 !pip install jieba
 !pip install wordcloud
 # window 命令行 进入相应的环境
 pip install jieba
 pip install wordcloud
# Linux系统默认字体文件路径
# !ls /usr/share/fonts/
# 查看系统可用的ttf格式中文字体
!fc-list :lang=zh | grep ".ttf"

# !wget https://mydueros.cdn.bcebos.com/font/simhei.ttf # 下载中文字体(下载有问题,可以通过本地上传字体)
#将字体文件复制到系统字体目录下
!cp simhei.ttf /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/
#aistudio上路径没有写权限 此方法不能用 !cp simhei.ttf /usr/share/fonts

# #创建字体目录fonts(系统字体w)
!mkdir .fonts
# # 复制字体文件到该路径
!cp simhei.ttf .fonts/
!rm -rf .cache/matplotlib/
#安装模型
!hub install porn_detection_lstm==1.1.0
!pip install --upgrade paddlehub #更新paddlehub

2.导入库

from __future__ import print_function
import requests
import json
import re #正则匹配
import time #时间处理模块
import jieba #中文分词
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
from PIL import Image
from wordcloud import WordCloud  #绘制词云模块
import paddlehub as hub

3.模块功能

3.1 请求接口

def getMovieinfo(url):
    '''
    请求爱奇艺评论接口,返回response信息
    参数  url: 评论的url
    :return: response信息
    '''
    session = requests.Session()
    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
        "Accept": "application/json",
        "Referer": "http://m.iqiyi.com/v_19rqriflzg.html",
        "Origin": "http://m.iqiyi.com",
        "Host": "sns-comment.iqiyi.com",
        "Connection": "keep-alive",
        "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6",
        "Accept-Encoding": "gzip, deflate"
    }
    response = session.get(url, headers=headers)
    if response.status_code == 200:
        return response.text
    return None

3.2 json数据解析

def saveMovieInfoToFile(lastId,arr):
    '''
    解析json数据,获取评论
    参数  lastId:最后一条评论ID  arr:存放文本的list
    :return: 新的lastId
    '''
    #更换url
    url="https://sns-comment.iqiyi.com/v3/comment/get_comments.action?agent_type=118&agent_version=9.11.5&authcookie=null&business_type=17&content_id=15472264800&page=&page_size=20&types=time&last_id="
    url+=str(lastId)
    responseTxt = getMovieinfo(url)
    responseJson=json.loads(responseTxt) #这个源的response对象是json文件格式 解析完后是一个str字典
    comments=responseJson['data']['comments']
    for val in comments:
        if 'content' in val.keys():
            arr.append(val['content'])
        lastId = str(val['id'])
    return lastId

3.3 数据清洗

def clear_special_char(content):
    """
    正则化处理一些字符
    content:爬取的单条评论
    return :清洗后的文本
    有表情, []
    """
    #compile 用于编译正则表达式,生成一个正则表达式对象(Pattern),供match()和search()连个函数使用
    comp = re.compile('[^A-Z^a-z^0-9^\u4e00-\u9fa5]') #除数字 英文 和 汉字外都不能数去。
    return comp.sub('', content) #去掉空格 ,需要执行编译对象

补充:是因为观察了分词后的数据添加

correct_dict = {
        '安琦': '安崎','a组':"A组",'b组':"B组"
    }
#增加 去除停用词(这一步可不要,新的分词表已包含)
new_add=["真的","这","已经","来","看起来","看","而","色","色色","色色色"]

with open("stopword.txt",'a')as f:
    for i in new_add:
        f.writelines(i+"\n")

3.4 jieba 分词

def fenci(text):
    """
    利用jieba进行分词
    text:需要分词的句子或文字
    return :分词结果
    """
    jieba.load_userdict('add_work.txt') #添加用户自定义的分词表
    text=jieba.lcut(text)
    for num,k in enumerate(text):
        if k in correct_dict.keys():
                text[num]=correct_dict[k]

    return text

3.5 创建并去除停用词表

def stopwordslist(file_path):
    """
    创建停用词表:
    file_path:停用词文本路径
    :return: 停用词list
    """
    stopwordslist=[]
    for line in open(file_path,encoding='UTF-8'):
        stopwordslist.append(line.strip())#去掉'\n
    return stopwordslist

def movespword(text,stopwords,counts):
    """
    去除停用词,统计词频
    text :评论由多个分词构成的列表[]
    stopwords:停用词
    list counts:词频统计结果
    :return:None
    """
    for i in text:
        if i not in stopwords:
            if len(i)!=1:#去掉一个字
                counts[i]=counts.get(i,0)+1
    return None

3.5 绘制词频统计表

def drawcounts(counts,top_N):
    """
    绘制词频统计表
    counts:
    top_N:保留最高词频的个数
    :return:
    """
    xvalue = []
    yvalue = []
    sort = sorted(counts.items(), key=lambda x: x[1], reverse=True)  # 默认从小到大
    for i in sort[:top_N]:
        xvalue.append(i[0])
        yvalue.append(i[1])

    # 绘制图形
    # 设置中文字体
    matplotlib.rcParams['font.sans-serif'] = ['SimHei']
    plt.bar(xvalue, yvalue)
    for a,b in zip(xvalue,yvalue):
        plt.text(a,b+0.05,'%d'%b,ha='center',va='bottom',fontdict={"size":10})
    plt.title('词频统计结果')
    plt.xlabel('高频词')
    plt.ylabel('词频')
    plt.show()

3.7 绘制词云图

def drawclod(counts):
    """
    根据词频绘制词云图
    :return:
    """
    #打开一张白底图片。图片必须是白底的,透明的也不行
    image=np.array(Image.open("aixin.png"))
    stopwords=['还是','有点','一直 ','还是','的话','就是','而且','啊啊啊']
    cloud=WordCloud(background_color='white',
                    mask=image,
                    max_words=200,
                    font_path='simhei.ttf',
                    min_font_size=10,
                    max_font_size=100,
                    relative_scaling='auto',
                    stopwords=stopwords
                    )
    cloud.fit_words(counts)
    cloud.to_file('pic.png')

3.8 内容审核

调用模型对评论内容进行审核,输出觉得晃晃(huanghuang)的评论

def text_detection(text, file_path):
    '''
    使用hub对评论进行内容分析
    return:分析结果

    '''
    porn_detection_lstm=hub.Module(name='porn_detection_lstm')
    f=open('record.txt','r',encoding='utf-8')
    for line in f:
        if len(line.strip())==1:
            continue
        else:
            test_text.append(line)
    f.close

    input_dict={'text':test_text}
    results=porn_detection_lstm.detection(data=input_dict,use_gpu=True,batch_size=64)
    for index, item in enumerate(results):
        if item['porn_detection_key'] =='porn':
            print(item['text'],':', item['porn_probs'])

4.执行

num=55 #num是页数,一般设置为一页20条评论。
lastId='0'
arr=[] #作为全局变量
with open('record.txt','a',encoding='utf-8') as f:
for i in range(num):
   lastId=saveMovieInfoToFile(lastId,arr)
   time.sleep(0.5)
print(len(arr))
error_num=0
# 写入数据
for item in arr:
   # 2.词频统计并可视化展示
   #进行数据清洗
   item=clear_special_char(item)
   #如果去除后不是为空:
   if item.strip()!='':#转化成str
       try:
           f.write(item+"\n")#分行
       except Exception as e:
           error_num+=1
           continue
print("抓取评论总数:",len(arr))
print("特殊符号评论未处理个数",error_num)
f=open('record.txt','r',encoding='utf-8')
counts={}
for line in f:
words=fenci(line)#读取逐行信息
stopwords=stopwordslist("stopword.txt")
#移除停用词
movespword(words,stopwords,counts)
# print(counts.items())
#counts.items()得到[('key',value),()]
drawcounts(counts,10)
drawclod(counts)
f.close()

在这里插入图片描述

display(Image.open('pic.png')) #显示生成的词云图像

在这里插入图片描述

file_path='record.txt'
test_text=[]
text_detection(test_text, file_path)

在这里插入图片描述

总结:

  1. 添加分词的方法:jieba.load_userdict(‘add_work.txt’) #添加用户自定义的分词表
  2. 数据清洗的规则制定 与 正则表达式
  3. 通过Network 监控网页行为
原创文章 4 获赞 8 访问量 394

猜你喜欢

转载自blog.csdn.net/qq_41838627/article/details/105821494
今日推荐