通过接口爬取全网音乐
本帖最后由 aa1448176630 于 2020-6-2 23:27 编辑import requests as r
import re
import os
def open_url(url):
data={'pagepwd': '11688'}# 11688 为进入网站口令 随时可能失效 自行更新
head={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 Edg/83.0.478.37'}
p=r.post(url,data=data,headers=head)
return p
def find_link(p):
p.encoding='utf-8'
p=p.text
l="url: '([^']+)"#播放链接
link=re.findall(l,p)
n="name: '([^']+)"#曲名
name=re.findall(n,p)
sn="artist: '([^']+)"#作者
sname=re.findall(sn,p)
#print(name)
#print(link)
save(name,link,sname)
def save(name,link,sname):
if not os.path.exists(r'C:\Users\Administrator\Desktop\allmusic'):
#print("ceshi")
os.mkdir(r'C:\Users\Administrator\Desktop\allmusic')
os.chdir(r'C:\Users\Administrator\Desktop\allmusic')
length=len(name)
for i in range(length):
if i>=5: #默认下前五首
break
path=r'C:\Users\Administrator\Desktop\allmusic'+'\\'+ name + sname + '.mp3'
with open (path,'ab+') as f:
p=open_url(link).content
f.write(p)
f.close()
print("第"+ str(i+1) + "首下载完成!")
print("默认前5首下载完成!")
if __name__=='__main__':
kw=input("请输入歌名或者歌手名:")
print("下载源有1、wy(网易云)2、kuwo(酷我)3、qq(QQ音乐)4、kg(酷狗)5、migu(咪咕)")
a=['wy','kuwo','qq','kg','migu']
flag=1
print("请输入下载源:",end='')
while flag:
source=input()
if source in a:
url='https://music.hwkxk.cn/?kw='+str(kw)+'&source='+str(source)
#print(url)
p=open_url(url)
find_link(p)
flag=0
else:
print("输入下载源有误,请重新输入:",end='')
爬虫新手,仅为交作业,大佬勿喷 唉,我还没学python呢,你们都好强大
页:
[1]