獲取特定歌曲熱評:
首先,我們打開網易云網頁版,擊排行榜,然后點擊左側云音樂熱歌榜,如圖:

關于如何抓取指定的歌曲的熱評,參考這篇文章,很詳細,對小白很友好:
手把手教你用Python爬取網易云40萬+評論
下圖是用上文的方法找到熱評后,確認下這條確實包含著熱評,hotComments就是我們要找的熱門評論:


到此為止,我們如何抓取網易云音樂的熱門評論已經分析完了,我們再分析一下如何獲取云音樂熱歌榜中所有歌曲的資訊,
獲取熱榜全部歌曲
同樣F12大法,這次選all,因為是找歌單,仔細觀察,很容易定位到這個嫌犯:toplist?id=3778678

點進去看下,預覽一下該請求回傳的結果,哈哈,就你啦!

heads:

request headers
現在看response:

太亂了,格式化一下,然后往下翻:


這樣就好找了,框框里面就是包含歌曲資訊的代碼,
因此,我們只需要將該請求的代碼中,將包含資訊的代碼篩選出來,
我們在這里使用正則運算式進行資料篩選,
通過觀察特點,我們可以通過兩次正則運算式的篩選,將我們需要的歌曲資訊提取出來,
第一次正則運算式如下:<ul class="f-hide"><li><a href="/song\?id=\d*?">.*</a></li></ul>
第二次正則運算式將需要的歌曲資訊提取出來,我們需要歌曲的歌名和id,對應的正則運算式如下:
獲取歌名:<li><a href="/song\?id=\d*?">(.*?)</a></li>
獲取歌曲的id:<li><a href="/song\?id=(\d*?)">.*?</a></li>
完整代碼:
#!/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'<ul class="f-hide"><li><a href="/song\?id=\d*?">.*</a></li></ul>' # 進行第一次篩選的正則運算式
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) # 獲取所有熱門歌曲對應的Id
return 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)
reponse = urllib.request.urlopen(request).read().decode('utf8')
json_dict = json.loads(reponse) # 獲取json
hot_commit = json_dict['hotComments'] # 獲取json中的熱門評論
num = 0
fhandle = open('./song_comments', 'a', encoding='utf-8') # 寫入檔案
fhandle.write(hot_song_name + ':' + '\n')
for item in hot_commit:
num += 1
fhandle.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
運行:

爬下來的:

對比:


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264490.html
標籤:python
