#!/usr/bin/env python3

-- coding: utf-8 --

import re
import urllib.request
import urllib.error
import urllib.parse

def get_all_hotSong(): #获取热歌榜所有歌曲名称
url=‘http://music.163.com/discover/toplist?id=3778678’ #网易云云音乐热歌榜url
header={ #请求头部
‘User-Agent’:‘Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36’
}
request=urllib.request.Request(url=url, headers=header)
html=urllib.request.urlopen(request).read().decode(‘utf8’) #打开url
html=str(html) #转换成str
pat1=r’

  • .*

’ #进行第一次筛选的正则表达式
result=re.compile(pat1).findall(html) #用正则表达式进行筛选
result=result[0] #获取tuple的第一个元素

pat2=r'<li><a href="/song\?id=\d*?">(.*?)</a></li>' #进行歌名筛选的正则表达式hot_song_name=re.compile(pat2).findall(result)    #获取所有热门歌曲名称return hot_song_name

def get_name(hot_song_name):
fhandle=open(’./song_name.txt’,‘a’,encoding=‘utf-8’) #写入文件
fhandle.write(hot_song_name+’\n’)
fhandle.close()
hot_song_name=get_all_hotSong() #获取热歌榜所有歌曲名称

num=0
while num < 192: #保存所有热门歌名
print(‘正在抓取第%d首歌…’%(num+1))
get_name(hot_song_name[num])
print(‘第%d首歌名抓取成功’%(num+1))
num+=1

#制作网易云热歌榜歌名词云
#!/usr/bin/env python3

-- coding: utf-8 --

“”"
Created on Mon Jun 11 20:34:46 2018

@author: Frances
“”"

import jieba.posseg as pseg
import matplotlib.pyplot as plt
from os import path

from scipy.misc import imread

from wordcloud import WordCloud
def extract_words():
with open(‘song_name.txt’,‘r’,encoding=‘utf-8’) as f:
hotsong_names = f.readlines()

stop_words = set(line.strip() for line in open('StopWords.txt',encoding='utf-8'))newslist = []for name in hotsong_names:if name.isspace():continue# segment words line by lineword_list = pseg.cut(name)     for word, flag in word_list:if not word in stop_words and flag == 'n':newslist.append(word)d = path.dirname(__file__)
mask_image = imread(path.join(d, "adorablecat.png"))content = ' '.join(newslist)
wordcloud = WordCloud(font_path='simhei.ttf', background_color="white", mask=mask_image, max_words=40).generate(content)# Display the generated image:
plt.imshow(wordcloud)
plt.axis("off")
wordcloud.to_file('wordcloud1.jpg')
plt.show()

if name == “main”:
extract_words()

-- coding: utf-8 --

“”"
Created on Sun Jun 10 16:59:36 2018

@author: Frances
“”"

#!/usr/bin/env python3

-- coding: utf-8 --

import re
import urllib.request
import urllib.error
import urllib.parse
import json

def get_all_hotSong(): #获取热歌榜所有歌曲名称和id
url=‘http://music.163.com/discover/toplist?id=3778678’ #网易云云音乐热歌榜url
header={ #请求头部
‘User-Agent’:‘Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36’
}
request=urllib.request.Request(url=url, headers=header)
html=urllib.request.urlopen(request).read().decode(‘utf8’) #打开url
html=str(html) #转换成str
pat1=r’

  • .*

’ #进行第一次筛选的正则表达式
result=re.compile(pat1).findall(html) #用正则表达式进行筛选
result=result[0] #获取tuple的第一个元素

pat2=r'<li><a href="/song\?id=\d*?">(.*?)</a></li>' #进行歌名筛选的正则表达式
pat3=r'<li><a href="/song\?id=(\d*?)">.*?</a></li>'  #进行歌ID筛选的正则表达式
hot_song_name=re.compile(pat2).findall(result)    #获取所有热门歌曲名称
hot_song_id=re.compile(pat3).findall(result)    #获取所有热门歌曲对应的Idreturn hot_song_name,hot_song_id

def get_hotComments(hot_song_name,hot_song_id):
url=‘http://music.163.com/weapi/v1/resource/comments/R_SO_4_’ + hot_song_id + ‘?csrf_token=’ #歌评url
header={ #请求头部
‘User-Agent’:‘Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36’
}
#post请求表单数据
data={‘params’:‘zC7fzWBKxxsm6TZ3PiRjd056g9iGHtbtc8vjTpBXshKIboaPnUyAXKze+KNi9QiEz/IieyRnZfNztp7yvTFyBXOlVQP/JdYNZw2+GRQDg7grOR2ZjroqoOU2z0TNhy+qDHKSV8ZXOnxUF93w3DA51ADDQHB0IngL+v6N8KthdVZeZBe0d3EsUFS8ZJltNRUJ’,‘encSecKey’:‘4801507e42c326dfc6b50539395a4fe417594f7cf122cf3d061d1447372ba3aa804541a8ae3b3811c081eb0f2b71827850af59af411a10a1795f7a16a5189d163bc9f67b3d1907f5e6fac652f7ef66e5a1f12d6949be851fcf4f39a0c2379580a040dc53b306d5c807bf313cc0e8f39bf7d35de691c497cda1d436b808549acc’}
postdata=urllib.parse.urlencode(data).encode(‘utf8’) #进行编码
request=urllib.request.Request(url,headers=header,data=postdata)
response=urllib.request.urlopen(request).read().decode(‘utf8’)
json_dict=json.loads(response) #获取json
hot_comment=json_dict[‘hotComments’] #获取json中的热门评论

num=0
fhandle=open('./Song_Comments.txt','a',encoding='UTF-8')  #写入文件
fhandle.write(hot_song_name+':'+'\n')for item in hot_comment:num+=1fhandle.write(str(num)+'.'+item['content']+'\n')
fhandle.write('\n==============================================\n\n')
fhandle.close()

hot_song_name,hot_song_id=get_all_hotSong() #获取热歌榜所有歌曲名称和id

num=0
while num < len(hot_song_name): #保存所有热歌榜中的热评
print(‘正在抓取第%d首歌曲热评…’%(num+1))
get_hotComments(hot_song_name[num],hot_song_id[num])
print(‘第%d首歌曲热评抓取成功’%(num+1))
num+=1

#制作网易云热歌榜热评词云

-- coding: utf-8 --

“”"
Created on Mon Jun 11 10:10:32 2018

@author: Frances
“”"

#!/usr/bin/env python3

-- coding: utf-8 --

“”"
Created on Mon Jun 11 10:01:11 2018

@author: fanzhengyi
“”"

import jieba.posseg as pseg
import matplotlib.pyplot as plt
from os import path

from scipy.misc import imread

from wordcloud import WordCloud
def extract_words():
with open(‘Song_Comments.txt’,‘r’,encoding=‘utf-8’) as f:
hotsong_comments = f.readlines()

stop_words = set(line.strip() for line in open('StopWords.txt',encoding='utf-8'))newslist = []for comment in hotsong_comments:if comment.isspace():continue# segment words line by lineword_list = pseg.cut(comment)     for word, flag in word_list:if not word in stop_words and flag == 'n':newslist.append(word)d = path.dirname(__file__)
mask_image = imread(path.join(d, "mickey.png"))content = ' '.join(newslist)
wordcloud = WordCloud(font_path='simhei.ttf', background_color="white", mask=mask_image, max_words=40).generate(content)# Display the generated image:
plt.imshow(wordcloud)
plt.axis("off")
wordcloud.to_file('wordcloud.jpg')
plt.show()

if name == “main”:
extract_words()

网易云热歌榜歌名与热评的高频词抓取及词云制作相关推荐

  1. Python爬取网易云热歌榜所有音乐及其热评

    获取特定歌曲热评: 首先,我们打开网易云网页版,击排行榜,然后点击左侧云音乐热歌榜,如图: 关于如何抓取指定的歌曲的热评,参考这篇文章,很详细,对小白很友好: 手把手教你用Python爬取网易云40万 ...

  2. python网易云热歌榜歌曲信息爬取(iframe框架内数据爬取,src为空)

    为一线医护人员加油! 为武汉加油! 为中国加油! 为世界加油! 此爬虫是本人参考了了一位前辈的文章,并修改和优化了代码: 1.改为python3环境: 2.优化了抓取的歌曲时长中带一长串小数的问题: ...

  3. python爬取网易云音乐飙升榜音乐_python爬取网易云音乐热歌榜 python爬取网易云音乐热歌榜实例代码...

    想了解python爬取网易云音乐热歌榜实例代码的相关内容吗,FXL在本文为您仔细讲解python爬取网易云音乐热歌榜的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,网易热歌榜 ...

  4. python爬取网易云音乐飙升榜音乐_python爬取网易云音乐热歌榜实例代码

    首先找到要下载的歌曲排行榜的链接,这里用的是: https://music.163.com/discover/toplist?id=3778678 然后更改你要保存的目录,目录要先建立好文件夹,例如我 ...

  5. python爬虫爬取音乐_利用python爬虫实现爬取网易云音乐热歌榜

    利用python爬虫实现爬取网易云音乐热歌榜 发布时间:2020-11-09 16:12:28 来源:亿速云 阅读:102 作者:Leah 本篇文章给大家分享的是有关利用python爬虫实现爬取网易云 ...

  6. python爬取歌曲_python爬取网易云音乐热歌榜实例代码

    首先找到要下载的歌曲排行榜的链接,这里用的是: https://music.163.com/discover/toplist?id=3778678 然后更改你要保存的目录,目录要先建立好文件夹,例如我 ...

  7. Python3---站在大佬肩膀写爬虫-爬取网易云音乐热歌榜歌曲热评(精彩评论)

    和我一起加入CSDN----程序猿和攻城狮的社区 网易云音乐是我比较喜欢的一个音乐平台,对于特别热爱听歌的人来说,网易云音乐精准的音乐定位和独特歌曲推荐,让人使用起来很舒服.所谓:自古评论出人才,精彩 ...

  8. python爬取音乐排行_python爬取网易云音乐热歌榜实例代码

    首先找到要下载的歌曲排行榜的链接,这里用的是: https://music.163.com/discover/toplist?id=3778678 然后更改你要保存的目录,目录要先建立好文件夹,例如我 ...

  9. 爬虫python代码网易云_python爬取网易云音乐热歌榜实例代码

    首先找到要下载的歌曲排行榜的链接,这里用的是: https://music.163.com/discover/toplist?id=3778678 然后更改你要保存的目录,目录要先建立好文件夹,例如我 ...

最新文章

  1. 2020-07-09 CVPR2020 VL论文讨论(4) 笔记
  2. Kafka科普系列 | 轻松理解Kafka中的延时操作
  3. Direct3D的一些小贴士收藏(转载)
  4. python切换消息窗_用Python切换窗口
  5. (七)Netty与零拷贝
  6. android视频录制无图像,Android开发拍摄视频在图库不显示问题
  7. 解除Xcode中Miss File的警告
  8. 判断鼠标向右或向左滑动,响应不同的事件
  9. ggplot2 多个柱状图比较_15. 再论ggplot2作图的图形元素组成
  10. 进入显示器工厂模式的方法 【95种品牌 维修珍藏资料】
  11. Greenplum 安装部署 单机版安装(Linux)
  12. PHP合并在线电影ts格式视频文件
  13. 微信小程序开发出现Page “pages/detail/detail” has not been registered yet.
  14. css实现两个div填满一行
  15. YouTorrent - 全新的“实时”BT种子搜索站
  16. AI-大型软件研发效能倍增的银弹
  17. Transformer对接公司需求的调研报告
  18. Florian%C3%B3polis巴西北岸新业务的最佳场所四方数据分析
  19. cannal 启动异常(show master status‘ has an error pls check. you need (at least one of) the SUPER,REPLI)
  20. python os.urandom()函数和十六进制\xhh的一些认识

热门文章

  1. 长江商学院营销学李洋教授分析大数据与精准营销
  2. win2012/2008取消禁ping
  3. 那一抹淡淡的汐蓝 (瀑瀑安)
  4. 在线Java 动态运行Java源代码-执行器
  5. 多线程应用_左圆右方
  6. 工作随记3:一次交换机环路故障
  7. 用python-turtle优雅的画椭圆
  8. 《FFmpeg从入门到精通》读书笔记(五)
  9. 表空间的相关查询命令
  10. Dynamics CRM 数据导出到Excel时列标题不能重复