-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.py
60 lines (59 loc) · 2.82 KB
/
Engine.py
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
import requests, json
from tqdm import tqdm
from you_get import common
from bs4 import BeautifulSoup
import urllib.request
headers = {'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; rv:11.0) like Gecko"}
def wyydownloader(ID):
'''利用官方API实现。'''
print('正在加载网易云音乐下载器:')
data = requests.get('http://music.163.com/api/song/detail/?id='+ID+'&ids=%5B'+ID+'%5D', headers=headers)
n = json.loads(data.content)
name = n['songs'][0]['name']
creator = n['songs'][0]['artists'][0]['name']
chunk_size = 1024
response = requests.get('http://music.163.com/song/media/outer/url?id=%s.mp3' % ID, headers=headers)
file_size = response.headers.get('Content-Length')
if file_size is not None:
file_size = int(file_size)
#部分代码Copy自 https://zhuanlan.zhihu.com/p/369531344
bar = tqdm(total=file_size, desc=f'下载文件: {name}',leave=True, unit='b', unit_scale=True, colour='#66ccff')
with open(f'{name} - {creator}.mp3', mode='wb') as f:
for chunk in response.iter_content(chunk_size=chunk_size):
f.write(chunk)
bar.update(chunk_size)
def bilibiliDownloader(url):
'''用you-get实现。'''
common.any_download(url, info_only=False,output_dir=r'保存的目录',merge=True)
def searcher(name):
f, c, d, k, _ = [], [], [], [], []
data = requests.get(r'http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s={'+name+r'}&type=1&offset=0&total=true&limit=20',headers=headers)
n = json.loads(data.content)['result']['songs'][0:9]
for x in range(len(n)):
f.append(n[x]['name'])
c.append(n[x]['artists'][0]['name'])
d.append(n[x]['id'])
for y in range(len(n)):
k.append([f[y],c[y],str(d[y])])
for z in range(len(n)):
_.append(('['+str(z)+']')+" | ".join(k[z]))
for __ in range(len(n)):
print(_[__])
a = input('请输入你选择下载的歌曲: ')
wyydownloader(str(d[int(a)]))
def playlist(ID):
headers = {'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',}
url="https://music.163.com/playlist?id=98332770"
response = requests.get(url=url, headers=headers)
html=response.content.decode(encoding="utf-8")
#print(html)
soup = BeautifulSoup(html, 'lxml')
results = soup.find('ul',{'class':'f-hide'})
results=results.find_all('a')
print(results)
for music in results:
#print(music.text, music['href'])
#下载歌曲
music_url="http://music.163.com/song/media/outer/url?id={}.mp3".format(music['href'].split("=")[1])
print(music_url)
urllib.request.urlretrieve(music_url,music.text+'.mp3')