你曾拥有一些英雄的梦想
好像黑夜里面温暖的灯光
怎能没有了希望的力量
只能够挺胸勇往直前
最近循环许巍的那一年,分享到朋友圈,有老板评论好久没听这首歌了,大概率是脱贫了,因为大部分循环听许巍的歌,总会印证那么一句话:贫穷是我们反复相遇。
言归正传,如果想要下载mp3歌曲,这里有一个网站推荐:歌曲宝(https://www.gequbao.com/),解析的各大平台的音乐,大致看了下网易云音乐应该是首选,这里本渣渣用Pyhton简单实现了下载mp3音乐及歌词,附上源码仅供参考和学习。
不知道此类网站能存活多久(找找还是有不少类似的解析平台音乐站点),且用且珍惜,爬取注意频率,不要乱搞!
# !/usr/bin/python
# -*- coding: UTF-8 -*-
#@author:huguo002
import requests
from lxml import etree
import time
import re
import sys
def get_musiclist(name):
url=f"https://www.gequbao.com/s/{name}"
headers={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
response=requests.get(url=url,headers=headers,timeout=8)
print(response.status_code)
html=response.content.decode("utf-8")
time.sleep(2)
tree=etree.HTML(html)
num=tree.xpath('//small[@class="float-right badge badge-pill badge-warning "]/text()')[0]
musicnum=num.replace('\n','').replace(' ','')
print(musicnum)
musicnames=tree.xpath('//div[@class="row"]/div[@class="col-5 col-content"]/a/text()')
print(musicnames)
musicsingers=tree.xpath('//div[@class="row"]/div[@class="text-success col-4 col-content"]/text()')
print(musicsingers)
musicurls=tree.xpath('//div[@class="row"]/div[@class="col-3 col-content"]/a/@href')
print(musicurls)
musiclists=[]
i=1
for musicname,musicsinger,musicurl in zip(musicnames,musicsingers,musicurls):
musiclist=i,musicname.replace('\n','').replace(' ',''),musicsinger.replace('\n','').replace(' ',''),'https://www.gequbao.com'+ musicurl.replace('\n','').replace(' ','')
print(musiclist)
musiclists.append(musiclist)
i=i+1
print(musiclists)
return musiclists
def get_music(musicurl):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
response = requests.get(url=musicurl, headers=headers, timeout=8)
print(response.status_code)
html = response.content.decode("utf-8")
#print(html)
time.sleep(2)
music_down_url=re.findall(r"const url = '(.+?)'.replace",html,re.S)[0]
print(music_down_url)
tree = etree.HTML(html)
down_name=tree.xpath('//span[@class="form-control bg-light overflow-hidden"]/text()')[0]
down_name=down_name.split()[0]
print(down_name)
musiclrc_down_url=tree.xpath('//div[@class="input-group-append"]/a[@id="btn-download-lrc"]/@href')[0]
musiclrc_down_url=f'https://www.gequbao.com{musiclrc_down_url}'
print(musiclrc_down_url)
down_music(down_name, music_down_url, musiclrc_down_url)
def down_music(down_name,music_down_url,musiclrc_down_url):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
music_response = requests.get(url=music_down_url, headers=headers, timeout=8)
print(music_response.status_code)
time.sleep(2)
with open(f"{down_name}",'wb') as f:
f.write(music_response.content)
print(f"{down_name}歌曲下载完成!")
music_lrc_name=f'{down_name[:-3]}lrc'
print(music_lrc_name)
music_lrc_response = requests.get(url=musiclrc_down_url, headers=headers, timeout=8)
print(music_lrc_response.status_code)
time.sleep(2)
with open(f"{music_lrc_name}",'wb') as f:
f.write(music_lrc_response.content)
print(f"{down_name}歌词下载完成!")
def main(name):
musiclists=get_musiclist(name)
for musiclist in musiclists:
musicurl=musiclist[3]
get_music(musicurl)
time.sleep(6)
sys.exit() #终止程序
if __name__=='__main__':
name = "那一年"
main(name)
运行:
运行效果:
源码设置了下载列表里的第一个歌曲,下载完即终止程序,有需求的可以自行更改,很久没写了,可能存在不少bug,就到这里吧,没什么好讲的。
附相关参考:
某网站自动下载音乐mp3和歌词 离线音乐-吾爱破解
https://www.52pojie.cn/thread-1734432-1-1.html
附上源码打包供参考:
关注本渣渣公众号
回复“歌曲宝”获取!!!
相关阅读:
Python tk例子,古董音乐播放器exe
VIP音乐下载器,Python Gui音乐下载exe工具
·················END·················
你好,我是二大爷,
革命老区外出进城务工人员,
互联网非早期非专业站长,
喜好python,写作,阅读,英语
不入流程序,自媒体,seo . . .
公众号不挣钱,交个网友。
读者交流群已建立,找到我备注 “交流”,即可获得加入我们~
听说点 “在看” 的都变得更好看呐~
关注关注二大爷呗~给你分享python,写作,阅读的内容噢~
扫一扫下方二维码即可关注我噢~
关注我的都变秃了
说错了,都变强了!
不信你试试
扫码关注最新动态
公众号ID:eryeji