1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| import requests from bs4 import BeautifulSoup import random, math from Crypto.Cipher import AES import base64 import codecs import json import time from lxml import html
etree = html.etree
class Spider(object): def __init__(self): self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0', 'Cookie':'_iuqxldmzr_=32; _ntes_nnid=8d4ef0883a3bcc9d3a2889b0bf36766a,1533782432391;' 'ntes_nuid=8d4ef0883a3bcc9d3a2889b0bf36766a;' 'utmc=94650624; WM_TID=GzmBlbRkRGQXeQiYuDVCfoEatU6VSsKC; playerid=19729878;' 'utma=94650624.1180067615.1533782433.1533816989.1533822858.9;' 'utmz=94650624.1533822858.9.7.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/;' 'WM_NI=S5gViyNVs14K%2BZoVerGK69gLlmtnH5NqzyHcCUY%2BiWm2ZaHATeI1gfsEnK%2BQ1jyP%2FROzbzDV0AyJ' 'HR4YQfBetXSRipyrYCFn%2BNdA%2FA8Mv80riS3cuMVJi%2BAFgCpXTiHBNHE%3D; WM_NIKE=9ca17ae2e6ffcda1' '70e2e6ee84b674afedfbd3cd7d98b8e1d0f554f888a4abc76990b184badc4f89e7af8ece2af0fea7c3b92a91eb' 'a9b7ec738e8abdd2b741e986a1b7e87a8595fadae648b0b3bc8fcb3f8eafb69acb69818b97ccec5dafee9682cb' '4b98bb87d2e66eb19ba2acaa5bf3b6b7b1ae5a8da6ae9bc75ef49fb7abcb5af8879f87c16fb8889db3ec7cbbae' '97a4c566e992aca2ae4bfc93bad9b37aab8dfd84f8479696a7ccc44ea59dc0b9d7638c9e82a9c837e2a3; JSES' 'SIONID-WYYY=sHwCKYJYxz6ODfURChA471BMF%5CSVf3%5CTc8Qcy9h9Whj6CfMxw4YWTMV7CIx5g6rqW8OBv04YGH' 'wwq%2B%5CD1N61qknTP%2Fym%2BHJZ1ylSH1EabbQASc9ywIT8YvOr%2FpMgvmm1cbr2%2Bd6ssMYXuTlpOIrKqp%5' 'C%2FM611EhmfAfU47%5CSQWAs%2BYzgY%3A1533828139236' }
def __get_mp3(self, id): d = '{"ids":"[%s]","br":320000,"csrf_token":""}' % id wyy = WangYiYun(d) data = wyy.get_data() url = 'https://music.163.com/weapi/song/enhance/player/url?csrf_token=' response = requests.post(url, data=data, headers=self.headers).json() return response['data'][0]['url']
def download_a_music(self, id): url = "http://music.163.com/api/song/lyric?"+"id="+str(id)+"&lv=1&kv=1&tv=-1" html = requests.post(url) json_obj = html.text j = json.loads(json_obj) try: lyric = j['lrc']['lyric'] except KeyError: lyric = "无歌词" url = self.__get_mp3(id) return url, lyric
class WangYiYun(object): def __init__(self, d): self.d = d self.e = '010001' self.f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5a" \ "a76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46be" \ "e255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7" self.g = "0CoJUm6Qyw8W8jud" self.random_text = self.get_random_str()
def get_random_str(self): str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' res = '' for x in range(16): index = math.floor(random.random() * len(str)) res += str[index] return res
def aes_encrypt(self, text, key): iv = '0102030405060708' pad = 16 - len(text.encode()) % 16 text = text + pad * chr(pad) encryptor = AES.new(key, AES.MODE_CBC, iv) msg = base64.b64encode(encryptor.encrypt(text)) return msg
def rsa_encrypt(self, value, text, modulus): text = text[::-1] rs = int(codecs.encode(text.encode('utf-8'), 'hex_codec'), 16) ** int(value, 16) % int(modulus, 16) return format(rs, 'x').zfill(256)
def get_data(self): params = self.aes_encrypt(self.d, self.g) params = self.aes_encrypt(params.decode('utf-8'), self.random_text) enc_sec_key = self.rsa_encrypt(self.e, self.random_text, self.f) return { 'params': params, 'encSecKey': enc_sec_key }
def music_msg(song_id, name, artist, cover): spider = Spider() url, lyric = spider.download_a_music(song_id) lyric = lyric.replace('"', "“") music_one_json = '{"name":"' + name + '","artist":"' + artist + '","url":"' + url + '","cover":"' + cover + '","lrc":"' + lyric + '"},' return music_one_json
def get_music_json(): url = 'https://music.163.com/#/playlist?id=2487763362' new_url = url.replace('/#', '') header = { 'Host': 'music.163.com', 'Referer': 'https://music.163.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0' } res = requests.get(new_url, headers=header).text r = BeautifulSoup(res, "html.parser") music_dict = {} result = r.find('ul', {'class', 'f-hide'}).find_all('a') for music in result: music_id = music.get('href').strip('/song?id=') music_name = music.text music_dict[music_id] = music_name music_json = "[" try: for song_id in music_dict: music_json += music_msg(song_id, music_dict[song_id], "J·Fla", "http://p4.music.126.net/79vdQ-WExvH5f_h3GvXcXQ==/109951163694979815.jpg?param=200y200") except Exception as e: print(e) return music_json[:-1] + "]"
def main(): while True: music_json = get_music_json().replace("\n", "\\n") with open('music.json', 'w',encoding='utf8') as fw: fw.write(music_json) print(music_json) music_json = "" time.sleep(600)
if __name__ == '__main__': main()
|