使用Python下载酷狗音乐

使用Python+Selenium+Urllib下载酷狗歌曲

最近想下载一首歌,找了各大音乐平台,觉得在酷狗上下载更容易。

首先是获取原音频地址(本文以野狼disco为例),存储在标签里的src属性中。

from selenium import webdriver as wd
kugo_url="https://www.kugou.com/song/#hash=B6880A36D02561912C53831ACF305D0D&album_id=30181741"
dr = wd.Chrome()
dr.get(c)

ele = dr.find_element_by_tag_name("audio")
url = ele.get_attribute("src")

然后就是下载了,本文用urllib下载。

from urllib.request import urlopen
import os
f1 = urlopen(url)
filename = f1.split('/')[-1]
with open(filename, 'wb') as f2:
	f2.write(f1.read())
print("音乐已保存在", os.path.join(os.getcwd(), filename))

大功告成!

完整代码:

from urllib.request import urlopen
import os
from selenium import webdriver as wd
kugo_url="https://www.kugou.com/song/#hash=B6880A36D02561912C53831ACF305D0D&album_id=30181741"
dr = wd.Chrome()
dr.get(c)

ele = dr.find_element_by_tag_name("audio")
url = ele.get_attribute("src")
f1 = urlopen(url)
filename = f1.split('/')[-1]
with open(filename, 'wb') as f2:
	f2.write(f1.read())
print("音乐已保存在", os.path.join(os.getcwd(), filename))

仅用于学习与交流,不得用于商业用途,否则后果自负

你可能感兴趣的:(使用Python下载酷狗音乐)