如何爬取网易云音乐用户听歌历史及喜欢的音乐?

lulunyaya 2017-04-22 10:56:54
首先声明,我只是个学生,只为研究不为任何不良企图。
网上搜了很多关于爬取网易云音乐的,但是全都是评论。
我也根据爬取评论的依法炮制如何爬取用户听歌历史及喜欢的音乐,但是根据别人的方法,爬取评论的话使用的是这个URL:
http://music.163.com/weapi/v1/resource/comments/R_SO_4_414826311?csrf_token=
在这个URL中,‘R_SO_4_’后面的数字就是歌曲的ID,通过歌曲的ID可以精准的爬取具体评论。
现在我要爬取用户听歌历史,我找到的是这个URL:
http://music.163.com/weapi/v1/play/record?csrf_token=
这个URL并不像评论URL那样带有歌曲ID信息,这个URL没有包含用户ID,爬取的时候出错:
{u'message': u"Required long parameter 'uid' is not present", u'code': 400}
所以我猜测,是因为代码中没有传入用户ID的原因,但是我学Python学的并不深入,代码也是别人爬取评论的代码,不知道如何才能成功爬取到用户听歌历史及喜欢的音乐,有大神赐教吗?
这只是个作业T^T有没有大神帮帮我
代码如下:
# encoding=utf8
import requests
from bs4 import BeautifulSoup
import os, json
import base64
from Crypto.Cipher import AES
from prettytable import PrettyTable
import warnings

warnings.filterwarnings("ignore")
BASE_URL = 'http://music.163.com/'


class Song(object):
def __lt__(self, other):
return self.commentCount > other.commentCount


def aesEncrypt(text, secKey):
pad = 16 - len(text) % 16
text = text + pad * chr(pad)
encryptor = AES.new(secKey, 2, '0102030405060708')
ciphertext = encryptor.encrypt(text)
ciphertext = base64.b64encode(ciphertext)
return ciphertext


def rsaEncrypt(text, pubKey, modulus):
text = text[::-1]
rs = int(text.encode('hex'), 16) ** int(pubKey, 16) % int(modulus, 16)
return format(rs, 'x').zfill(256)

def createSecretKey(size):
return (''.join(map(lambda xx: (hex(ord(xx))[2:]), os.urandom(size))))[0:16]


def matchSong():
url = 'http://music.163.com/weapi/v1/play/record?csrf_token='
headers = {
'Cookie': 'appver=1.5.0.75771;',
'Referer': 'http://music.163.com/'
}
text = {
'username': '',
'password': '',
'rememberLogin': 'true'
}
modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
nonce = '0CoJUm6Qyw8W8jud'
pubKey = '010001'
text = json.dumps(text)
secKey = createSecretKey(16)
encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
encSecKey = rsaEncrypt(secKey, pubKey, modulus)
data = {
'params': encText,
'encSecKey': encSecKey
}
req = requests.post(url, headers=headers, data=data)
data = req.json()
print data
# total = req.json()['total']
# if int(total) > let:
# song = Song()
# song.id = songId
# song.commentCount = total
# return song

def main():
matchSong()
print 'End'


if __name__ == '__main__':
main()
...全文
755 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
likebutterfly 2020-05-17
  • 打赏
  • 举报
回复
请问你爬取成功了吗
  • 打赏
  • 举报
回复
同求,我想要把用户听歌历史通过js嵌入网页中。但是js一直不能得到数据。
qq_32442819 2017-06-21
  • 打赏
  • 举报
回复
这个做得怎么样了 我最近也在搞
lulunyaya 2017-04-22
  • 打赏
  • 举报
回复
没有人吗

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧