爬网易云音乐动态的坑

  • 最近闲来无事,慢慢刷网易云动态视频,发现了一个小姐姐动态视频 小姐姐动态主页,感觉唱的歌挺好听的.人也可爱,就想把视频爬下来做成音频.这样免得每次想听歌都要看视频.省麻烦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:下次再爬多页 以及拿取下载视频链接

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值