【一、專案目標】
通過手把手教你使用Python抓取QQ音樂資料(第一彈)我們實作了獲取 QQ 音樂指定歌手單曲排行指定頁數的歌曲的歌名、專輯名、播放鏈接,
通過手把手教你使用Python抓取QQ音樂資料(第二彈)我們實作了獲取 QQ 音樂指定歌曲的歌詞和指定歌曲首頁熱評,
此次我們在專案(二)的基礎上獲取更多評論并生成詞云圖,形成手把手教你使用Python抓取QQ音樂資料(第三彈),
【二、需要的庫】
主要涉及的庫有:requests、json、wordcloud、jieba
如需更換詞云圖背景圖片還需要numpy庫和PIL庫(pipinstall pillow)
【三、專案實作】
1.首先回顧一下,下面是專案(二)獲取指定歌曲首頁熱評的代碼;
def get_comment(i):
url_3 = 'https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg'
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
/# 標記了請求從什么設備,什么瀏覽器上發出
}
params = {'g_tk_new_20200303': '5381', 'g_tk': '5381', 'loginUin': '0', 'hostUin': '0', 'format': 'json', 'inCharset': 'utf8', 'outCharset': 'GB2312', 'notice': '0', 'platform': 'yqq.json', 'needNewCode': '0', 'cid': '205360772', 'reqtype': '2', 'biztype': '1', 'topid': id, 'cmd': '8', 'needmusiccrit': '0', 'pagenum': '0', 'pagesize': '25', 'lasthotcommentid': '', 'domain': 'qq.com', 'ct': '24', 'cv': '10101010'}
res_music = requests.get(url_3,headers=headers,params=params)
/# 發起請求
js_2 = res_music.json()
comments = js_2['hot_comment']['commentlist']
f2 = open(i+'評論.txt','a',encoding='utf-8') /#存盤到txt中
for i in comments:
comment = i['rootcommentcontent'] + '\n——————————————————————————————————\n'
f2.writelines(comment)
/# print(comment)
f2.close()
2.下面來考慮如何獲取后面的評論,下圖是專案(二)評論頁面的parms引數;

3.網頁無法選擇評論的頁碼,想看后面的評論智能一次一次的點擊“點擊加載更多”;我們可以點擊一下看看parms有什么變化,
4.這里有個小技巧,先點擊下圖所示clear按鈕,把network界面清空,再點擊“點擊加載更多”,就能直接找到第二頁的資料,

5.點擊加載更多后出現下圖,

6.發現不止pagenum變了,cmd和pagesize也變了,到底那個引數的問題呢,那我們再看下第三頁;

7.只有pagenum變了,那我們嘗試一下把pagenum改成“0”,其他不變,能正常顯示第一頁資料嗎?

第一頁第一條評論

第一頁最后一條評論,
8.能正常顯示,那就確定思路了:用第二頁的parms,寫一個for回圈賦值給pagenum,參考專案(二)把評論抓取到txt,
9.代碼實作:為了不給服務器造成太大壓力,我們本次只爬取20頁資料,
import requests,json
def get_id(i):
global id
url_1 = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
/# 這是請求歌曲評論的url
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
params = {'ct': '24', 'qqmusic_ver': '1298', 'new_json': '1', 'remoteplace': 'txt.yqq.song', 'searchid': '71600317520820180', 't': '0', 'aggr': '1', 'cr': '1', 'catZhida': '1', 'lossless': '0', 'flag_qc': '0', 'p': '1', 'n': '10', 'w': i, 'g_tk': '5381', 'loginUin': '0', 'hostUin': '0', 'format': 'json', 'inCharset': 'utf8', 'outCharset': 'utf-8', 'notice': '0', 'platform': 'yqq.json', 'needNewCode': '0'}
res_music = requests.get(url_1,headers=headers,params=params)
json_music = res_music.json()
id = json_music['data']['song']['list'][0]['id']
return id
/# print(id)
def get_comment(i):
url_3 = 'https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
f2 = open(i+'評論.txt','a',encoding='utf-8') /#存盤到txt中
for n in range(20):
params = {'g_tk_new_20200303': '5381', 'g_tk': '5381', 'loginUin': '0', 'hostUin': '0', 'format': 'json', 'inCharset': 'utf8', 'outCharset': 'GB2312', 'notice': '0', 'platform': 'yqq.json', 'needNewCode': '0', 'cid': '205360772', 'reqtype': '2', 'biztype': '1', 'topid': '247347346', 'cmd': '6', 'needmusiccrit': '0', 'pagenum':n, 'pagesize': '15', 'lasthotcommentid': 'song_247347346_3297354203_1576305589', 'domain': 'qq.com', 'ct': '24', 'cv': '10101010'}
res_music = requests.get(url_3,headers=headers,params=params)
js_2 = res_music.json()
comments = js_2['comment']['commentlist']
for i in comments:
comment = i['rootcommentcontent'] + '\n——————————————————————————————————\n'
f2.writelines(comment)
/# print(comment)
f2.close()
input('下載成功,按回車鍵退出!')
def main(i):
get_id(i)
get_comment(i)
main(i = input('請輸入需要查詢歌詞的歌曲名稱:'))
10.詞云圖代碼
from wordcloud import WordCloud
import jieba
import numpy
import PIL.Image as Image /#以上兩個庫是為了更換詞云圖背景圖片
def cut(text):
wordlist_jieba=jieba.cut(text)
space_wordlist=" ".join(wordlist_jieba)
return space_wordlist
with open("句號評論.txt" ,encoding="utf-8")as file:
text=file.read()
text=cut(text)
mask_pic=numpy.array(Image.open("心.png"))
wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf",
collocations=False,
max_words= 100,
min_font_size=10,
max_font_size=500,
mask=mask_pic).generate(text)
image=wordcloud.to_image()
/# image.show()
wordcloud.to_file('云詞圖.png') /# 把詞云保存下來
11.成果展示

【四、總結】
1.專案三比專案二多的功能:一是通過尋找parms引數里每一頁評論頁碼之間的關系,爬取更多的評論;二是學會生成詞云圖;(注意讀取檔案的路徑)
2.WordCloud更多引數詳見下圖,可以研究出更多的玩法;

- 不只.txt可以作為詞云圖的資料源,csv、Excel也可以:
import xlrd
/#引入excel讀取模塊
datafile_path = '你的Excel檔案.xlsx'
data = https://www.cnblogs.com/dcpeng/p/xlrd.open_workbook(datafile_path)
/#檔案名以及路徑
table = data.sheet_by_name('sheet')
/#/#通過名稱獲取Sheet1表格
nrows = table.nrows
/#獲取該Sheet1中的有效行數
list = []
for i in range(nrows):
value = https://www.cnblogs.com/dcpeng/p/str(table.row_values(i)[1])
/# print(value)
list.append(value)
/# print(pingjia_list)
text = str(list).replace("'", '').replace(',', '').rstrip(']').lstrip('[')
/# print(text)
4.爬QQ音樂專案到此告一段落,如有需要的話可以通過Scrapy框架爬取更多的歌曲資訊、歌詞、評論,但是作為練手專案,重要的不是爬多少資料,而是學會如何爬取指定的資料,
5.第四彈小編將會把前面三個專案封裝在一起,通過選單控制爬取不同資料,敬請期待,
6.需要本文原始碼的話,請在公眾號后臺回復“QQ音樂”四個字進行獲取,
看完本文有識訓?請轉發分享給更多的人
IT共享之家
入群請在微信后臺回復【入群】

想學習更多Python網路爬蟲與資料挖掘知識,可前往專業網站:http://pdcfighting.com/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/109019.html
標籤:Python
上一篇:day10__回顧和作業
下一篇:cut方法的使用
