天天看點

Python 20行代碼實作全民k歌下載下傳分享音樂

注:需要下載下傳相關的包

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歌下載下傳分享音樂