python⽂本挖掘与分析:歌曲《说散就散》⽹易云⾳乐平台⽤
户评论分析
⼀、准备⼯作:
1.python3.x
2.编辑器pycharm
import requests,json,os
import base64
import codecs
from Crypto.Cipher import AES
import pymysql#可加可不加
⼆、数据源:《说散就散》⽹易云⾳乐评论区,本⽂选取了jc和袁娅维两个版本的《说散就散》,爬取她们各⾃的评论进⾏⽂本分析产⽣词云图,两张词云图的对⽐就会发现⼀些秘密了。因为⾃从原唱登上好声⾳后,对于jc⼈们是越来越熟悉了,⽹上说原唱⽐翻唱好听,更有青春感,也有⼈说袁娅维唱的才最好听,到底答案是什么呢?我想⼤众的眼光总是没错的,我们可以知道这⾸歌⽕起来时我们⼤多数⼈并不知道是jc的原唱,所以作者想着对⽐分析⼀下各⾃的歌曲评论,看能发现出什么不。
1.⽹易云⾳乐⽹页分析:
可以知道的是评论区的数据是以json的格式存储,20个为⼀组,进⼀步分析得post请求有两个参数需要⼀起传递过去才能访问该⽹址,由那些编码可知这是加过密的数据,这⾥就不说解密过程了,篇幅有限,也可以直接⽤这两个参数,但是其他页⾯就同样需要这样分析得出这
两个参数。
下⾯为爬⾍类,⽤于获取数据:
class Spider():
def __init__(self):
self.header = {'User-Agent':'xxxxxxxxx',
'Referer': 'music.163/'}
self.url = 'xxxxxxxxxxxx'  #post请求的⽹址
def __get_jsons(self,url,page):
music = WangYiYun()
text = ate_random_16()
params = _params(text,page)
encSecKey = _encSEcKey(text)
fromdata = {'params' : params,'encSecKey' : encSecKey}
jsons = requests.post(url, data=fromdata, headers=self.header,verify=False)
def json_list(self,jsons):
users = json.loads(jsons)
用c++语言编写的代码程序
comments = []
for user in users['comments']:
name = user['user']['nickname']
content = user['content']
goodnum = user['goodnum']
user_dict = {'name': name, 'content': content, 'goodnum': goodnum}
comments.append(user_dict)
return comments
def write_sql(self,comments):
music = Operate_SQL()
print('第%d页正在获取' % self.page)
for comment in comments:
#print(comment)
music.add_data(comment)
print('  该页获取完成')
def run(self):
self.page = 1
while True:
jsons = self.__get_jsons(self.url,self.page)python请求并解析json数据
comments = self.json_list(jsons)
asp应用程序源码print(comments[0])
# 当这⼀页的评论数少于20条时,证明已经获取完
self.write_sql(comments)
if len(comments) < 20:
print('评论已经获取完')
break
self.page +=1
下⾯为存⼊mysql数据库和程序启动:
# 操作 mysql
class Operate_SQL():
# 连接数据库
def __get_conn(self):
try:layui前端框架使用详解
< = t(host='127.0.0.1',user='root',passwd='xxxx',port=3306,db='xxxx',charset='utf8mb4')        except Exception as e:
print(e, '数据库连接失败')
def __close_conn(self):
'''关闭数据库连接'''
try:
:
except pymysql.Error as e:
print(e, '关闭数据库失败')
通配符怎么打出来exceldef add_data(self,comment):
sql = 'INSERT INTO `gumeng`(`name`,`content`,`goodnum`) VALUE(%s,%s,%s)'
try:
self.__get_conn()
cursor = ursor()
#table = "comments"  # 要操作的表格
# 注意,这⾥查询的sql语句url=' %s '中%s的前后要有空格
#sqlExit = "SELECT content FROM comments  WHERE content = ' %s '" % (comment['content'])
#res = ute(sqlExit)
#if not res:  # res为查询到的数据条数如果⼤于0就代表数据已经存在
return 1
#else:
#return 0
except AttributeError as e:
print(e,'添加数据失败')
return 0
except pymysql.DataError as e:
print(e)
return 0
finally:
if cursor:
cursor.close()
self.__close_conn()
# 出post的两个参数params和encSecKey
class WangYiYun():
def __init__(self):
# 在⽹易云获取的三个参数
self.second_param = '010001'
self.third_param = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152
b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fc          self.fourth_param = '0CoJUm6Qyw8W8jud'
def create_random_16(self):
'''获取随机⼗六个字母拼接成的字符串'''
return (''.join(map(lambda xx: (hex(ord(xx))[2:]), str(os.urandom(16)))))[0:16]
def aesEncrypt(self, text, key):
iv = '0102030405060708'
pad = 16 - len(text) % 16
text = text + pad * chr(pad)
encryptor = w(key, 2, iv)jquery ui datepicker只要年份
ciphertext = pt(text)
ciphertext = base64.b64encode(ciphertext)
return ciphertext
def get_params(self,text,page):
if page == 1:
self.first_param = '{rid: "R_SO_4_400162138", offset: "0", total: "true", limit: "20", csrf_token: ""}'
else:
self.first_param = ('{rid: "R_SO_4_400162138", offset:%s, total: "false", limit: "20", csrf_token: ""}'%str((page-1)*20))
params = self.aesEncrypt(self.first_param, self.fourth_param).decode('utf-8')
params = self.aesEncrypt(params, text)
return params
def rsaEncrypt(self, pubKey, text, modulus):
text = text[::-1]
rs = de('utf-8'), 'hex_codec'), 16) ** int(pubKey, 16) % int(modulus, 16)
return format(rs, 'x').zfill(256)
def get_encSEcKey(self,text):
'''获取第⼆个参数'''
pubKey = self.second_param
moudulus = self.third_param
encSecKey = self.rsaEncrypt(pubKey, text, moudulus)
return encSecKey
def main():
spider = Spider()
spider.run()
if __name__ == '__main__':
⾄此爬⾍完成,没有运⾏成功的可以联系我,LJ642615662。
三、清洗数据:
清洗数据可以先从mysql数据库导出到Excel然后去除重复项,选择评论列保存到txt⽂件⾥,也可以⽤python直接来清洗数据。获取到的数据:
预处理后的数据:
四、⽂本分析,此次分析部分的源码和上篇《延禧攻略》分析差不多就不过多阐述了,想要源码的可以去我的上篇博⽂中。
经过导⼊,分词,去停⽤词,画词云图后可得下图:
这个为jc的评论分析词云图,数据量11万