網易雲音樂 抓取成功,按評論數排序


 

可以生成外鏈播放器

<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=298 height=52 src="//music.163.com/outchain/player?type=2&id= 151646&auto=0&height=32"></iframe>

需要保存的項有:
m_name :歌名
m_singer:歌手
vote 評論數
url : 鏈接
播放功能
通過 相似歌曲查找其它歌,
歌名
//*[@id="auto-id-xRQFDgyFzdwbznWT"]/div[3]/div[1]/div/div/div[1]/div[1]/div[2]/div[1]/div/em
//*[@id="auto-id-sQbhxsxtnM7vJTti"]/div[3]/div[1]/div/div/div[1]/div[1]/div[2]/div[1]/div/em
//*[@id="auto-id-koka4uE8EU8gmROm"]/div[3]/div[1]/div/div/div[1]/div[1]/div[2]/div[1]/div/em
/song?id=29431270
相似歌曲是相對路徑:
豆瓣查看網頁源代碼有xpath,但是雲音樂沒有
<a href =" /song?id=393705 " title =" 英雄的黎明 "
爬取的是框架外的代碼。
不是iframe里面的代碼
<iframe name="contentFrame" id="g_iframe" class="g-iframe" scrolling="auto" frameborder="0" src="about:blank"></iframe>
注意有2個iframe
先下載網頁源代碼,注意不是框架源代碼。
分析:
js

selenium和 phantomjs
iframe content is not returned as a part of html.
The problem is that iframe content is not returned as a part of html. You can either try to fetch iframe content directly (by its src), or use render.json endpoint with iframes=1 option:
import parsel
# ... yield SplashRequest(url, self.parse_result, endpoint='render.json', args={'html': 1, 'iframes': 1}) def parse_result(self, response): iframe_html = response.data['childFrames'][0]['html'] sel = parsel.Selector(iframe_html) item = { 'my_field': sel.xpath(...), # ... }
/execute  endpoint doesn't support fetching iframes content as of Splash 2.3.3.
note that xpath shouldn't include anything outside iframe - process iframe content as a separate document
Also, you may need to take a different iframe; I wrote  ['childFrames'][0]  as an example - index could be different.
If the web page has mutiple iframes, and iframe you're interested in is not the first, you'll have to use an appropriate index instead of 0
注意
title=[u'\u6545\u4e61\u7684\u539f\u98ce\u666f']是一個元組,
列表用 [ ] 標識
如果想要打印出來,需要加index
logging.info("title=%s" % sel.xpath('//div[@class="tit"]/em/text()').extract()[0])
找歌手
//div[@class="cnt"]/p[1]/span/@title
p1表示是div下面的第一個p元素,注意序號是從1開始的,
而且選取屬性用@title,不是@title/text()

現在抓取了其ifame里面的頁面,但是里面的評論數沒有!
<i><span id="cnt_comment_count">評論</span></i>
正常應該是
<i>(<span id="cnt_comment_count">14115</span>)</i>
評論框也沒有
<div class="n-cmt" id="comment-box" data-tid="R_SO_4_393705" data-count="0"> </div>
這個鏈接是
附:
爬評論分析過程
chrome network 選擇xhr過濾下,
Initiator  標記請求是由哪個對象或進程發起的
如果看不到initiator,點擊關閉
這個請求是一個post,有兩個參數,params和encSecKey
那么這兩個參數是怎么獲得的呢?
從initiator一欄里可以看到這個請求的“發起人”是core.js
一般這樣的js都是沒法看的,下載下來美化過后發現有兩萬多行,但是沒關系,我們需要的只是部分數據。
在這個js文件中搜索params和encSecKey
那么問題就變成得到這個bua
它是由window.asrsea這個函數得到的,可以看到有4個參數, 如果研究每個參數肯定是痛苦的,也沒有必要,可以先 把它們輸出來看一下,這時候就需要 線上調試js,我選擇了Fiddler,在Fiddler的AutoResponder頁添加Rule,大概長這樣
之后 網頁加載使用的core.js文件 就是我們本地的這個js文件了,而我們可以修改本地的這個文件來獲得想要的數據
如果有多個輸出,就在頁面的Headers中找到encSecKey的值,與控制台輸出的對比下,就可以找到真正j50的輸出了。
就是下面這條了!
發現rid就是R_SO_4_加上歌曲的id
offset就是(評論頁數-1) * 20
按這樣的方式可以得到 其余三個參數
如 第二個參數:
010001
第三個參數:
00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7
第四個參數:
0CoJUm6Qyw8W8jud
現在我們只要知道函數window.asrsea如何處理的就可以了
定位到這個函數發現它其實是一個叫d的函數
這里的i研究之后你會發現就是一個長度為16的隨機字符串,既然是隨機的,我就直接讓他等於16個F了
這個encText明顯就是params,encSecKey明顯就是encSecKey
而b函數就是一個AES加密,經過了兩次加密,
第一次對d也就是那個json加密,key是第四個參數,第二次對第一次加密結果進行加密,key是i。在b函數中我們可以看到
密鑰偏移量iv是0102030405060708,模式是CBC,那么就不難寫出對於這個json的加密了。
接下來是第二個參數encSecKey,
c (i, e,f)
這里傳入c的三個參數i是16個F,e是第二個參數,f是第三個參數(值見上),全部是固定的值
那么無論歌曲id或評論頁數如何變化,這個 encSecKey都不隨之發生變化
encSecKey:
3de07243e358cf9b6d5398c72774adcb086446650b4dd385df00a5331ae8358448d051fbe8efe36bb8f4eb22f61fd13e4db484b3fa516c81fd8c1e64058a0838b866f4e6adb449a7acb918dd02e63e795e48e99cb4c9edc57a0a7d79fa480a8d71cf679fe16ccd4e2472425f300c91ebff15e61407891b867da1d3cc8842e8ce
至此,我們得到了所有的兩個參數。
No module named Crypto.Cipher
sudo pip uninstall crypto
sudo pip uninstall pycrypto
sudo pip install pycrypto
算法先通過createSecretKey生成一個16位的隨機字符串作為密鑰secKey,然后將明文text進行兩次AES加密獲得密文encText,因為secKey是在客戶端上生成的,所以還需要對其進行RSA加密再傳給服務端。
url=http://music.163.com/#/song?id=393705
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM