某网站自动下载音乐mp3和歌词 离线音乐

想下载点离线音乐在车里听歌了,发现各大因为app都要会员,于是朋友发给我一个网站。可是进去发现需要一个一个下载,嫌麻烦,于是····开工

python 复制代码
from selenium import webdriver
import requests
from bs4 import BeautifulSoup
import os
options = webdriver.ChromeOptions()
options.add_argument('--headless')
# 给请求指定一个请求头来模拟chrome浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
    'cookie':'__51uvsct__JZKGLNhr7gNTmF1z=1; __51vcke__JZKGLNhr7gNTmF1z=dbcc8135-b908-58b9-ab0f-f09772cc8ef9; __51vuft__JZKGLNhr7gNTmF1z=1673170099915; __vtins__JZKGLNhr7gNTmF1z=%7B%22sid%22%3A%20%2250340dc9-526b-5b41-8642-2fa520c011a5%22%2C%20%22vd%22%3A%2030%2C%20%22stt%22%3A%204104371%2C%20%22dr%22%3A%20616811%2C%20%22expires%22%3A%201673176004282%2C%20%22ct%22%3A%201673174204282%7D'
}
server = 'https://www.gequbao.com'
# 凤凰传奇地址
singer = 'https://www.gequbao.com/s/%E8%B4%B9%E7%8E%89%E6%B8%85'
 
 
# 获取歌曲内容
def get_contents(song,song_title,singer_name):
    # print(song)
    save_url = 'G:/python/songs/{}'.format(singer_name)
    save_lrc_path = 'G:/python/songs/{}/{}.lrc'.format(singer_name,song_title)
    res = requests.get(url=song, headers=headers)
    res.encoding = 'utf-8'
    html = res.text
    soup = BeautifulSoup(html, 'html.parser')
    # 获取歌曲的下载链接
    driver = webdriver.Chrome(options=options)
    driver.get(song)
 
    song_elem = driver.find_element_by_id("btn-download-mp3")
    lrc_elem = driver.find_element_by_id("btn-download-lrc")
    download_url = song_elem.get_attribute('href')
    lrc_url = lrc_elem.get_attribute('href')
    # 读取MP3资源
    req = requests.get(download_url, stream=True)
    # 文件夹不存在,则创建文件夹
    folder = os.path.exists(save_url)
    if not folder:
        os.makedirs(save_url)
 
    # 文件存储地址
    full_title = song_title + '.mp3'
    file_path = os.path.join(save_url, full_title)
    print('开始写入歌曲:', file_path)
    # 打开本地文件夹路径file_path,以二进制流方式写入,保存到本地
    with open(file_path, 'wb') as fd:
        for chunk in req.iter_content():
            fd.write(chunk)
    print(song_title + '成功下载!')
    # 下载歌词
    print('开始写入歌词:', save_lrc_path)
    r = requests.get(lrc_url).content
    with open(save_lrc_path, 'wb') as fd:
        fd.write(r)
        fd.close()
    print(song_title + '歌词成功下载!')
 
 
# 主方法
def main():
    res = requests.get(singer, headers=headers)
    res.encoding = 'utf-8'
    html = res.text
    # 使用自带的html.parser解析
    soup = BeautifulSoup(html, 'html.parser')
    # 获取歌曲的列表
    songs = soup.find('div', class_='card-text').find_all(class_='text-primary')
    singer_name = soup.find('input', id='s-input-line')['value']
    print('查询到歌曲数: %d ' % len(songs))
    for each in songs:
        try:
            song = server + each.get('href')
            song_title = each.get_text().strip()
            get_contents(song,song_title,singer_name)
        except Exception as e:
            print(e)
 
 
if __name__ == '__main__':
    main()

运行之后需要输入要下载的歌手搜索结果页

下载速度有点慢,而且访问太频繁系统会返回443,但是满足我的需求了,所以也没继续优化,瞎玩呗

相关推荐
烛阴4 小时前
简单入门Python装饰器
前端·python
好开心啊没烦恼4 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
面朝大海,春不暖,花不开4 小时前
使用 Python 实现 ETL 流程:从文本文件提取到数据处理的全面指南
python·etl·原型模式
2301_805054566 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python
万千思绪6 小时前
【PyCharm 2025.1.2配置debug】
ide·python·pycharm
微风粼粼7 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
云天徽上8 小时前
【PaddleOCR】OCR表格识别数据集介绍,包含PubTabNet、好未来表格识别、WTW中文场景表格等数据,持续更新中......
python·ocr·文字识别·表格识别·paddleocr·pp-ocrv5
你怎么知道我是队长8 小时前
python-input内置函数
开发语言·python
叹一曲当时只道是寻常8 小时前
Python实现优雅的目录结构打印工具
python
hbwhmama9 小时前
python高级变量XIII
python