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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| import io import sys import os import re import json import requests
from lxml import etree from bs4 import BeautifulSoup from datetime import datetime
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
def songs_lists(url=None): if url is None: url = 'https://music.163.com/#/playlist?id=2781114174' url = url.replace('/#', '').replace('https', 'http') headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 'Referer': 'https://music.163.com/', 'Host': 'music.163.com' } res = requests.get(url=url, headers=headers).text
out_link = 'http://music.163.com/song/media/outer/url?id='
tree = etree.HTML(res) song_list = tree.xpath('//ul[@class="f-hide"]/li/a') artist_name_tree = tree.xpath('//h2[@id="artist-name"]/text()') artist_name = str(artist_name_tree[0]) if artist_name_tree else None
song_list_name_tree = tree.xpath('//h2[contains(@class,"f-ff2")]/text()') song_list_name = str(song_list_name_tree[0]) if song_list_name_tree else None
song_ids = [],names = [],song_links =[] for i, s in enumerate(song_list): href = str(s.xpath('./@href')[0])
song_id = href.split('=')[-1] song_ids.append(song_id)
src = out_link + song_id song_links.append(src)
title = str(s.xpath('./text()')[0]) names.append(title)
return [song_ids, names, song_links]
def download_song(lists, download_location): if lists[2]: print('开始下载音乐')
if not os.path.exists(download_location): os.mkdir(download_location)
for n, song in enumerate(lists[2]): print('正在下载第{}首歌曲!'.format(n) + ':' + lists[1][n]) name = download_location + '/' + lists[1][n] + '.mp3' try: data = requests.get(song).content with open(name, 'wb') as f: f.write(data) except Exception as e: print(e) print('{}首全部歌曲已经下载完毕!'.format(len(lists[1])))
def download_comment(lists=None, download_location =None,pages =10): if lists[2]: print('开始获取评论!')
headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 'Referer': 'https://music.163.com/', 'Host': 'music.163.com' }
for n1, list in enumerate(lists[0]): link = 'https://music.163.com/api/v1/resource/comments/R_SO_4_' + list for l in range(1,pages,1): url = link + '?offset=' + str(l) print('评论链接来源:'+url) res = requests.get(url=url, headers=headers).text json_obj = json.loads(res) comments = json_obj['comments'] print('正在下载第{}首歌曲的评论!'.format(n1) + ':' + lists[1][n1])
if not os.path.exists(download_location): os.mkdir(download_location)
for i, n in enumerate(comments): comment = n['content'] nickname = n['user']['nickname'] time_update = n['time'] time = datetime.fromtimestamp(time_update//1000) time = time.strftime('%Y-%m-%d %H:%M:%S') name_comment = download_location + '/' + lists[1][n1] + '.txt' try: with open(name_comment, 'a+') as f: data = ('\t' + nickname + ':' + '\t' + time + '\n' + comment+'\n') f.write(data + '\n') except Exception as e: print(e) print('{}首全部歌曲评论的前{}页已经下载完毕!'.format(len(lists[1]),pages))
def download_lyrics(lists, download_location): headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 'Referer': 'https://music.163.com/', 'Host': 'music.163.com' } song_name, song_id = lists[1], lists[0] for i, id in enumerate(song_id): url = 'http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1'.format(id) print(url) try: res = requests.get(url=url, headers=headers).text json_obj = json.loads(res) lyric = json_obj['lrc']['lyric'] reg = re.compile(r'\[.*\]') lrc_text = re.sub(reg, '', lyric).strip() except Exception as e: print(e) if not os.path.exists(download_location): os.mkdir(download_location) name = download_location + '/' + song_name[i] + '.lrc' try: with open(name, 'w') as f: data = lrc_text.replace('\xa0','') f.write(data) except Exception as e: print(e)
def songs_lists_html(link): path =link file = open(path, encoding='utf-8') context = file.read() file.close()
context.encode('utf-8')
soup = BeautifulSoup(context, "html.parser") song_links =[] links = [] song_ids = [] names = [] res = soup.select("tr") out_link = r'http://music.163.com/song/media/outer/url?id=' for li in res: title = li.find_all("b") link = li.find_all("a") try: new_link = r'http://music.163.com' + link[0].attrs['href'] song_links.append(new_link) except: pass for i in title: out = i.attrs['title'].replace('\xa0', '') names.append(out) for n in song_links: link = n.replace('/song?', '/song/media/outer/url?').replace('https', 'http') links.append(link) id = n.replace('http://music.163.com/song?id=', '') song_ids.append(id) return [song_ids, names, links]
if __name__ == '__main__':
url = 'https://music.163.com/#/playlist?id=2781114174' link = r'./mylist.html' pages = 10 info = songs_lists_html(link)
names = info[1][0:10] links = info[2][0:10] ids = info[0][0:10] lists = [ids, names, links]
download_location = r'./cloudmusic' download_song(lists,download_location) download_comment(lists,download_location,pages) download_lyrics(lists,download_location)
|