天天看點

爬網易雲音樂動态的坑

爬網易雲音樂動态的坑

    • 先給幾章大佬的文章,讓我成為一個代碼的搬運工>..<
    • 1.ModuleNotFoundError: No module named 'Crypto'
    • 2.TypeError: Object type
  • 最近閑來無事,慢慢刷網易雲動态視訊,發現了一個小姐姐動态視訊 小姐姐動态首頁,感覺唱的歌挺好聽的.人也可愛,就想把視訊爬下來做成音頻.這樣免得每次想聽歌都要看視訊.省麻煩0.0

    但是作為一個Python萌新,想爬網易雲還是有點麻煩0.0

先給幾章大佬的文章,讓我成為一個代碼的搬運工>…<

https://segmentfault.com/a/1190000012818254 Python爬蟲之網易雲音樂下載下傳

https://blog.csdn.net/tzs_1041218129/article/details/52789153 網易雲音樂登入資訊加密算法詳解

https://www.zhihu.com/question/36081767/answer/140287795 知乎— 如何爬網易雲音樂的評論數?

https://segmentfault.com/a/1190000015958460 用Python代碼來下載下傳任意指定網易雲歌曲(超詳細版)

。。。。。。

具體的流程我不想再多說,幾篇大佬的文章講的比我還優秀,如果和我一樣的或者比我還萌新的,就先去看看吧!

那我就先談談我碰到的坑QAQ…

1.ModuleNotFoundError: No module named ‘Crypto’

from Crypto.Cipher import AES
ModuleNotFoundError: No module named 'Crypto'
           

W( ̄_ ̄)W紅紅的顔色真欠扁

因為在大佬文章中已經說明了,網易雲的請求方式為post請求,post請求位址而post請求時還需要兩個加密參數’params’: {…} ‘encSecKey’: {…}

爬網易雲音樂動态的坑

若不送出這兩個加密參數. 将無法得到伺服器傳回的資料 空白一片 (至于 查詢字元串 csrf_token 這個參數這次的文章暫不考慮,下一次再讓我扳明白)

爬網易雲音樂動态的坑

當然這可以自己在浏覽器裡複制粘貼這兩個加密參數’params’: {…} ‘encSecKey’: {…}——————

但是0.0這比剛剛我想不看視訊隻聽歌更麻煩 而如果想要在Python中讓代碼自己得到這兩個參數0.0,,,那麼crypto子產品就可以提供了加密功能 這裡就會得到我們的問題

1.ModuleNotFoundError: No module named ‘Crypto’

如果在cmd中安裝 pip instal Crypto

爬網易雲音樂動态的坑

是會有安裝的資訊,也可以安裝成功 但是相信我你還是用不了 0.0 因為 好像它停用了( ̄~ ̄)

爬網易雲音樂動态的坑

最後在我多番努力查找下 pip install pycryptodeme 才會是你的真愛 o(一︿一+)o 你根本不用再做什麼需求什麼安裝,可以這很符合我的性格

爬網易雲音樂動态的坑

2.TypeError: Object type <class ‘str’> cannot be passed to C code

raise TypeError("Object type %s cannot be passed to C code" % type(data))
	TypeError: Object type <class 'str'> cannot be passed to C code
           

這裡我們翻譯一下 str對象類型不能傳遞給C代碼

(⊙ˍ⊙) 什麼鬼 我可是對大佬的代碼什麼都沒動的,,,為什麼就報錯了那????複制的代碼

仔細看看出問題的地方 )

爬網易雲音樂動态的坑

在定義函數的

def aesEncrypt(text, secKey):
	pad = 16 - len(text) % 16
	#print(type(text))
	#print(type(pad))
	#print(type(pad * chr(pad)))
	#text = text + str(pad * chr(pad))
#這裡有轉換type和str,上面的print是為了看清楚類型
	if isinstance(text,bytes):
		print("type(text)=='bytes'")
		text=text.decode('utf-8')
		#print(type(text))
	text = text  + str(pad * chr(pad))
	encryptor = AES.new(secKey, 2, '0102030405060708')
	ciphertext = encryptor.encrypt(text)
	ciphertext = base64.b64encode(ciphertext)
	return ciphertext

           

可以先看看這些資料類型

爬網易雲音樂動态的坑

.(○´・д・)ノ 你這不全是str類型的嗎?

最後想了個辦法全部改成byte類型資料,可行至于為什麼下次再詳解

最後貼上改完的代碼

import requests
import json
import os
from Crypto.Cipher import AES
import base64
import codecs
import re

def aesEncrypt(text, secKey):
    iv = '0102030405060708'#偏移量
    pad = 16 - len(text) % 16
    if isinstance(text,bytes):
        text=text.decode('utf-8')
    text = text  + str(pad * chr(pad))
    text = text.encode()
    if isinstance(secKey,str):
        secKey=secKey.encode()
    iv=iv.encode()
    encryptor = AES.new(secKey,AES.MODE_CBC, iv)
    ciphertext = encryptor.encrypt(text)
    ciphertext = base64.b64encode(ciphertext)
    return ciphertext

def rsaEncrypt(text, pubKey, modulus):
    text = text[::-1]
#hex不是這麼用	rs = int(text.encode('hex'), 16)**int(pubKey, 16)%int(modulus, 16)
    rs = int(codecs.encode(text.encode('utf-8'),'hex_codec'), 16)**int(pubKey, 16)%int(modulus, 16)
    return format(rs, 'x').zfill(256)

modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
nonce = '0CoJUm6Qyw8W8jud'
pubKey = '010001'
#後三個參數固定

def createSecretKey(size):
    return (''.join(map(lambda xx: (hex(ord(xx))[2:]), os.urandom(size))))[0:16]
#自己登入 網易雲的賬戶密碼
text = {
			'username': '',
			'password': '',
			'rememberLogin': 'true'
		}
text = json.dumps(text)
secKey = createSecretKey(16)
#傳回一個16随機字元
encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
encSecKey = rsaEncrypt(secKey, pubKey, modulus)
##上面兩部進行加密
payload = {
			'params': encText,
			'encSecKey': encSecKey
		}
headers = {
    'Cookie':'{#︿( ̄︶ ̄)︿自己加自己的}',
    'Referer':'https://music.163.com/user/event?id=1786326513',
    'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36 XiaoBai/10.0.1813.254'
    }

url = "https://music.163.com/weapi/event/get/1786326513?csrf_token=1feab1a5bce31c683e676730284f4b8b"

html = requests.post(url,data=payload,headers=headers)
json_datas=json.loads(html.text)['events']
for json_data in json_datas:
    try:
        id_list=json.loads(json_data["json"])["video"]['videoId']
        name_list=json.loads(json_data["json"])["video"]['title']
        print(id_list,name_list)     
    except:
        print("這條動态未發現視訊")



           

就此感謝那些爬蟲大佬的付出,新萌還在學習的路上

Ps:下次再爬多頁 以及拿取下載下傳視訊連結