))#删除停用词defremove_stop_words(f):stop_words=['Lyricist','Composer','Arranger','Arranger','Recording','Mixer','Voice','Vocal','String','Keyboard','Keyboard','Editor','Assistant','Assistants','Mixing','Editing','Recording','Music','Production','Producer','Publish','produced','and','distributed']forstop_wordinstop_words:f=f.replace(stop_word,"")returnf#获取指定艺术家页面前50首歌曲ID和歌曲名称defget_songs(artist_id):page_url='https://music.163.com/artist?id='+artist_id#获取网页htmlres=requests.request('GET',page_url,headers=headers)#使用xpath解析前50首歌曲信息html=etree.HTML(res.text)href_xpath="//*[@id='hotsong-list']//a/@href"name_xpath="//*[@id='hotsong-list'']//a/text()"#href_xpath="//*[@id='hotsong-list']//a/@href"#name_xpath="//*[@id='hotsong-list']//a/text()"hrefs=html.xpath(href_xpath)names=html.xpath(name_xpath)#设置流行so的IDng,歌曲名称song_ids=[]song_names=[]forhref,nameinzip(hrefs,names):song_ids.append(href[9:])song_names.append(name)print(href,'',name)returnsong_ids,song_names#生成词云defcreat_word_cloud(f):print("根据词频,开始生成词云!")f=remove_stop_words(f)cut_text="".join(jieba.cut(f,cut_all=False,HMM=True))wc=WordCloud(font_path="./wc.ttf",max_words=100,width=2000,height=1200,)print(cut_text)wordcloud=wc.generate(cut_text)#写入词云文件wordcloud.to_file("artist_wordcloud.jpg")#显示词云文件plt.imshow(wordcloud)plt.axis("off")plt.show()#设置歌手ID,邓紫棋是7763artist_id='7763'[song_ids,song_names]=get_songs(artist_id)#所有歌词all_words=''#获取每首歌的歌词(song_id,song_name)inzip(song_ids,song_names):#lyricsapiurllybric_url='http://music.163.com/api/song/lyric?os=pc&id='+song_id+'&lv=-1&kv=-1&tv=-1'lyric=get_song_lyric(headers,lybric_url)all_words=all_words+""+lyri
