Skip to content

Commit f01788b

Browse files
committed
add code
1 parent d08d916 commit f01788b

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
from lxml import etree
2+
import requests
3+
import os
4+
import logging
5+
6+
log_format = '%(asctime)s => %(message)s '
7+
url = 'https://music.163.com/discover/toplist'
8+
hd = {
9+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
10+
}
11+
12+
download_dir = os.path.join(os.getcwd(), "download_songs/")
13+
14+
15+
def set_logger():
16+
logger = logging.getLogger()
17+
logger.setLevel(logging.INFO)
18+
formatter = logging.Formatter(fmt=log_format, datefmt='%Y-%m-%d %H:%M:%S')
19+
20+
ch = logging.StreamHandler()
21+
ch.setLevel(logging.DEBUG)
22+
ch.setFormatter(formatter)
23+
logger.addHandler(ch)
24+
25+
return logger
26+
27+
28+
def down_song_by_song_id_name(id, name):
29+
if not os.path.exists(download_dir):
30+
os.mkdir(download_dir)
31+
url = 'http://music.163.com/song/media/outer/url?id={}.mp3'
32+
r = requests.get(url.format(id), headers=hd)
33+
is_fail = False
34+
try:
35+
with open(download_dir + name + '.mp3', 'wb') as f:
36+
f.write(r.content)
37+
except:
38+
is_fail = True
39+
logger.info("%s 下载出错" % name)
40+
if (not is_fail):
41+
logger.info("%s 下载完成" % name)
42+
43+
44+
def get_topic_ids():
45+
r = requests.get(url, headers=hd)
46+
html = etree.HTML(r.text)
47+
nodes = html.xpath("//ul[@class='f-cb']/li")
48+
logger.info('{} {}'.format('榜单 ID', '榜单名称'))
49+
ans = dict()
50+
for node in nodes:
51+
id = node.xpath('./@data-res-id')[0]
52+
name = node.xpath("./div/p[@class='name']/a/text()")[0]
53+
ans[id] = name
54+
logger.info('{} {}'.format(id, name))
55+
return ans
56+
57+
58+
def get_topic_songs(topic_id, topic_name):
59+
params = {
60+
'id': topic_id
61+
}
62+
r = requests.get(url, params=params, headers=hd)
63+
html = etree.HTML(r.text)
64+
nodes = html.xpath("//ul[@class='f-hide']/li")
65+
ans = dict()
66+
logger.info('{} 榜单 {} 共有歌曲 {} 首 {}'.format('*' * 10, topic_name, len(nodes), '*' * 10))
67+
for node in nodes:
68+
name = node.xpath('./a/text()')[0]
69+
id = node.xpath('./a/@href')[0].split('=')[1]
70+
ans[id] = name
71+
logger.info('{} {}'.format(id, name))
72+
73+
return ans
74+
75+
76+
def down_song_by_topic_id(id, name):
77+
ans = get_topic_songs(id, name)
78+
logger.info('{} 开始下载「{}」榜单歌曲,共 {} 首 {}'.format('*' * 10, name, len(ans), '*' * 10))
79+
for id in ans:
80+
down_song_by_song_id_name(id, ans[id])
81+
82+
83+
logger = set_logger()
84+
85+
86+
def main():
87+
ids = get_topic_ids()
88+
while True:
89+
print('')
90+
logger.info('输入 Q 退出程序')
91+
logger.info('输入 A 下载全部榜单歌曲')
92+
logger.info('输入榜单 Id 下载当前榜单歌曲')
93+
94+
topic_id = input('请输入:')
95+
96+
if str(topic_id) == 'Q':
97+
break
98+
elif str(topic_id) == 'A':
99+
for id_x in ids:
100+
down_song_by_topic_id(id_x, ids[id_x])
101+
else:
102+
print('')
103+
ans = get_topic_songs(topic_id, ids[topic_id])
104+
print('')
105+
logger.info('输入 Q 退出程序')
106+
logger.info('输入 A 下载全部歌曲')
107+
logger.info('输入歌曲 Id 下载当前歌曲')
108+
song_id = input('请输入:')
109+
if str(song_id) == 'Q':
110+
break
111+
elif str(song_id) == 'A':
112+
down_song_by_topic_id(topic_id, ids[topic_id])
113+
else:
114+
down_song_by_song_id_name(song_id, ans[song_id])
115+
116+
117+
if __name__ == "__main__":
118+
main()
119+

doudou/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Python技术 公众号文章代码库
1515
+ [lagou](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-07-13-lagou):拉钩招聘数据分析
1616
+ [national-day](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-10-13-national-day):国庆旅游热图
1717
+ [Appium](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-10-20-appium):Appium 神器
18+
+ [163 music](https://github.com/JustDoPython/python-examples/tree/master/doudou/2020-11-02-163-music):下载网易云乐库
1819

1920
---
2021

0 commit comments

Comments
 (0)