python利用网易云音乐接口搭建的音乐推荐,根据单曲歌名推荐相关用户喜爱的歌曲

该博客介绍如何利用Python结合网易云音乐接口,通过搜索功能获取歌曲ID,然后分析歌曲热门评论用户的歌单数据,对歌曲进行加权评分,并筛选出高分歌曲进行推荐。博主分享了接口获取和评分计算的代码,并提供了简单的Tkinter操作界面。
摘要由CSDN通过智能技术生成

一、网易云音乐的相关接口

这边我想要的数据接口有:

* 网易的搜索功能,根据歌名获取歌曲的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
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值