前言
大家好,我是南南
众所周知,作为一个单身lsp,b站舞蹈区探花,咳咳咳,直接进入正题吧
由于内容过多,爬虫代码csdn上有很多篇博客讲解,我就不写了(我是不会告诉你是我懒的)
视频地址:BV12q4y1S7j9
爬完的数据是一个xlsx文件点我下载,我们直接开始数据分析吧!
提取评论写入txt文档
# -*- coding:utf-8 -*-
import re
from openpyxl import load_workbook#打开一个新文件
wb = load_workbook(filename = r"C:\Users\Administrator\Desktop\ss.xlsx")
data = open("b.txt",'w',encoding="utf-8")#建立文档
ws.delete_rows(1)#删除第一行\
ws = wb.get_sheet_by_name('sheet1')#打开工作表1
first_column = ws['D']#第四列
for i in range(len(first_column)):
print(first_column[i].value,file=data) #读取单元格内容并存储为文本
导入相关库
#!/usr/bin/python
# encoding: UTF-8
import re
import jieba
import wordcloud
import pyecharts.options as opts
import pandas as pd
逐行读取文本为str
data = ''
with open('b.txt',errors='ignore',encoding='utf-8') as f:#逐行读取文本为str
for line in f.readlines():
line = line.strip()
data += line
print(data)
去除符号&分词
from string import punctuation
str = data
add_punc=',。、【】“”:;()《》‘’{}?!⑦()、%^>℃:.”“^-——=擅长于的&#@¥' # 去除字符串内的符号
all_punc = punctuation + add_punc
temp = []
for c in str:
if c not in all_punc :
temp.append(c)
newText = ''.join(temp)
print(newText)
#分词
import jieba
mytext = " ".join(jieba.cut(newText))
print(mytext)
词云可视化
from wordcloud import WordCloud
wordcloud = WordCloud(font_path="msyh.ttc",background_color='white').generate(mytext)
%pylab inline
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
运行结果
写在最后
感觉python还是蛮简单的,有兴趣的可以学一学python基础语法