网易云音乐歌单制作词云图

首先附上制作的网易云词云图

有了虾米的经验,今天准备制作网易云歌单,同理,我也去XHR里面找了一下。

画对勾的,其响应为一个html,里面包含了我想要的歌曲信息,手动保存该html。


我想要的信息又在html的<body></body>里面,通过bt4获取该标签的文本内容text。
而我想要的歌名 歌手名 其在text中的形式为: "name":"歌名" "name":"歌手名"
即我只要匹配 "name":"xxx" 即可

=================代码================================

# -*- coding: utf-8 -*-
"""
Created on Sun Feb 23 08:47:10 2020

@author: leslielee


import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
url = 'https://music.163.com/#/my/m/music/playlist?id=321080292'
response = requests.get(url,headers=headers)
print(response.status_code)
"""

from bs4 import BeautifulSoup
import re
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
import numpy as np
from PIL import Image
import collections # 词频统计库

path = 'D:/Documents/python/wordcloud/detail.html'

# 获取html内容
html = BeautifulSoup(open(path,encoding='utf-8'),features='html.parser')

# 提取 <body></body>里面的文本内容
body = html.body.text

pattern = re.compile(r'"name":"(.*?)"')
names =  pattern.findall(body)


#词频统计
word_counts = collections.Counter(names) # 对分词做词频统计

# 词频展示
mask = np.array(Image.open('D:/Documents/python/wordcloud/madonna.jpg')) # 定义词频背景
wc = WordCloud(
    background_color='white', # 设置背景颜色
    mask=mask, # 设置背景图
    max_words=250, # 最多显示词数
    random_state=42,
    max_font_size=150, # 字体最大值
    scale=32, #图片清晰度
    font_path="C:/Windows/Fonts/STXINGKA.TTF" # 解决显示口字型乱码问题
    )


# 生成词云
wc.generate_from_frequencies(word_counts) 

image_colors = ImageColorGenerator(mask, default_color=(255,23,140))
wc.recolor(color_func=image_colors)

wc.to_file('D:/Documents/python/wordcloud/songs7.jpg')

注:
1.系统字体文件路径
C:/Windows/Fonts/

2.csdn的图片上传怎么弄? 我只会截图复制粘贴。

3.看人家的博客园页面制作的很不错。

发布了53 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37083038/article/details/104457544