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 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
import re import json import base64 import codecs import requests from Crypto.Cipher import AES from requests.exceptions import RequestException
FILE_NAME = 'wyyyy.md' URL163 = ['http://music.163.com/#/song?id=186001', 'http://music.163.com/#/song?id=437250607', 'http://music.163.com/#/song?id=4172700', 'http://music.163.com/#/song?id=30953009' ]
class Netease(object):
def __init__(self): self.header = { "Referer": 'http://music.163.com/', 'Cookie': 'appver=1.5.0.75771;', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36', }
def get_id(self, url): pattern = re.compile('\d+\d', re.S) song_id = re.findall(pattern, url) return song_id[1]
def get_html(self, url): request_url = 'http://music.163.com/song?id=' + self.get_id(url) headers = self.header try: response = requests.get(request_url, headers=headers) if response.status_code == 200: return response.text return None except RequestException: return None
def get_title(self, url): title_html = self.get_html(url) pattern = re.compile('(?<=title\>).*(?=</title)', re.S) title = re.findall(pattern, title_html) title[0].encode('utf-8') return title[0]
def AES_encrypt(self, text, key, iv): pad = 16 - len(text) % 16 text = text + chr(pad) * pad encryptor = AES.new(key, AES.MODE_CBC, iv) encrypt_text = encryptor.encrypt(text) encrypt_text = base64.b64encode(encrypt_text) return encrypt_text
def get_params(self): iv = "0102030405060708" first_param = "{rid:\"\", offset:\"0\", total:\"true\", limit:\"20\", csrf_token:\"\"}" forth_param = "0CoJUm6Qyw8W8jud" first_key = forth_param second_key = 16 * 'F' h_encText = self.AES_encrypt(first_param, first_key, iv) h_encText = self.AES_encrypt(h_encText, second_key, iv) return h_encText
def get_csrf_token(self): csrf_token = '1c51dc5cedf74268eaedb744f431f27b' return csrf_token
def get_encSecKey(self): encSecKey = "257348aecb5e556c066de214e531faadd1c55d814f9be95fd06d6bff9f4c7a41f831f6394d5a3fd2e3881736d94a02ca919d952872e7d0a50ebfa1769a7a62d512f5f1ca21aec60bc3819a9c3ffca5eca9a0dba6d6f7249b06f5965ecfff3695b54e1c28f3f624750ed39e7de08fc8493242e26dbc4484a01c76f739e135637c" return encSecKey
def get_json(self, song_id, csrf_token, params, encSecKey): base_url = 'http://music.163.com/weapi/v1/resource/comments/R_SO_4_' request_url = base_url + song_id + '?csrf_token=' + csrf_token headers = self.header post_data = { "params": params, "encSecKey": encSecKey } try: response = requests.post(request_url, headers=headers, data=post_data) if response.status_code == 200: return response.json() return None except RequestException: return None
def save_as_json(self, song_name, json_result): line = json.dumps(json_result, ensure_ascii=False) file_name = song_name + '.json' with codecs.open(file_name, 'w', 'utf-8') as fp: fp.write(line)
def set_title(self): global FILE_NAME with codecs.open(FILE_NAME, 'w', 'utf-8') as fp: fp.write(u'## 网易云音乐热门评论')
def save_title(self, song_name): global FILE_NAME with codecs.open(FILE_NAME, 'a', 'utf-8') as fp: fp.write('\n### ') fp.write(song_name)
def save_comment(self, json_data): global FILE_NAME hot_comment = json_data['hotComments'] print("total:", len(hot_comment)) for i in range(len(hot_comment)): with codecs.open(FILE_NAME, 'a', 'utf-8') as fp: fp.write('\n@( ') fp.write(hot_comment[i]['user']['nickname']) fp.write(' )') with codecs.open(FILE_NAME, 'a', 'utf-8') as fp: fp.write('\n> ') fp.write(hot_comment[i]['content']) fp.write('\n')
def claw(self, url_list): self.set_title() for url in url_list: song_id = self.get_id(url) song_name = self.get_title(url) self.save_title(song_name) print(song_name) csrf_token = self.get_csrf_token() params = self.get_params() encSecKey = self.get_encSecKey() json_data = self.get_json(song_id, csrf_token, params, encSecKey) self.save_comment(json_data) self.save_as_json(song_id, json_data)
def main(): netease = Netease() netease.claw(URL163)
if __name__ == "__main__": main()
|