一个简单的网页歌曲批量下载脚本,初学python,内容比较基础~
[Python] 纯文本查看 复制代码 import requests
import re
import urllib.request
from bs4 import BeautifulSoup
response = urllib.request.urlopen("https://www.9ku.com/douyin/shenqu.htm")
content =response.read().decode('utf8')
soup = BeautifulSoup(content,"html.parser")
# l1 =soup.select('a[target="_1"][class="songName"]')
# print(len(l1))
l2 =soup.find_all('a', {'href': re.compile('/play/'),'target':'_1'})
print("共扫描到"+str(len(l2))+"首歌曲,准备下载...")
i=0
def get_music_url(purl):
"""获取下载链接"""
r = requests.get(purl)
soup = BeautifulSoup(r.text, "lxml")
try:
surl = soup.find('a', {'style':'display:none'})['href']
stitle = soup.find('li', text=re.compile('mp3下载,')).text
sname = stitle[0:stitle.find('mp3下载,')]
return surl,sname
except Exception as e:
print(e)
print(purl)
def download_music(sname, surl):
"""下载音乐"""
response = requests.get(surl)
content = response.content
save_file(sname+'.mp3', content)
print("保存成功:"+sname+'.mp3')
def save_file(filename, content):
"""保存音乐"""
with open(file=filename, mode="wb") as f:
f.write(content)
for sno in l2:
# print("https://www.9ku.com" + sno["href"])
purl = "https://www.9ku.com" + sno["href"].replace('play','down')
try:
surl,sname = get_music_url(purl)
download_music(sname, surl)
i=i+1
print(i)
except Exception as e:
print(e)
|