注:需要下载相关的包
Python可打包成exe文件在电脑上运行(貌似只有Python3.6及以下的版本才能打包,否则打包的exe无法运行)
import json
from bs4 import BeautifulSoup
import urllib.request
from pip._vendor import requests
url=input(\'请输入链接:\');
res=urllib.request.Request(url)
with urllib.request.urlopen(res) as response:
data=response.read().decode()
soup = BeautifulSoup(data, "html.parser")
javascript=soup.select(\'script\').pop(2).string.strip(\'window.__DATA__ = \');
javascript=javascript.rstrip(\';\')
json_data=json.loads(javascript);
downloadlink=json_data.get(\'detail\').get(\'playurl\');
singer=json_data.get(\'detail\').get(\'nick\');
song_name=json_data.get(\'detail\').get(\'song_name\');
f = requests.get(downloadlink)
pass
filename=str(singer)+\'-\'+str(song_name)+".mp3";
with open(song_name+".mp3", "wb") as code:
code.write(f.content);
print(filename+\'.mp3保存成功\')
运行结果:
Python 20行代码实现全民k歌下载分享音乐
Python 20行代码实现全民k歌下载分享音乐