一、网易云音乐的相关接口
这边我想要的数据接口有:
* 网易的搜索功能,根据歌名获取歌曲的id
* 歌曲相关的评论用户接口
* 用户的相关数据包括歌单或听歌记录,这边听歌记录的接口好像不能用,所以我就用的歌单接口
关于每个接口大家可以自己F12网易官网看看是长什么样子,网易的新接口加密方式我也是在网上找的资料。
这边就是接口部分的代码:
import requests
import json
import os
import base64
import binascii
from Crypto.Cipher import AES
class NetEaseAPI:
def __init__(self):
self.header = {
'Accept': '*/*',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'music.163.com',
'Referer': 'http://music.163.com/search/',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36' # NOQA
}
self.cookies = {'appver': '1.5.2'}
self.playlist_class_dict = {}
self.session = requests.Session()
def httpRequest(self, method, action, query=None, urlencoded=None, callback=None, timeout=None):
connection = json.loads(self.rawHttpRequest(method, action, query, urlencoded, callback, timeout))
return connection
def rawHttpRequest(self, method, action, query=None, urlencoded=None, callback=None, timeout=None):
if method == 'GET':
url = action if query is None else action + '?' + query
connection = self.session.get(url)
elif method == 'POST':
connection = self.session.post(action, query, self.header)
elif method == 'Login_POST':
connection = self.session.post(action, query, self.header)
self.session.cookies.save()
connection.encoding = 'UTF-8'
return connection.text
def search(self, s, stype=1, offset=0, total='true', limit=1):
action = 'http://music.163.com/api/search/get'
data = {
's': s,
'type': stype,
'offset': offset,
'total': total,
'limit': limit
}
return self.httpRequest('POST', action, data)
def aesEncrypt(self, text, secKey):
pad = 16 - len(text) % 16
text = text + chr(pad) * pad
encryptor = AES.new(secKey, 2, '0102030405060708')
ciphertext = encryptor.encrypt(text)
ciphertext = base64.b64encode(ciphertext).decode('utf-8')
return ciphertext
def rsaEncrypt(self, text, pubKey, modulus):
text = text[::-1]
rs = pow(int(binascii.hexlify(text), 1