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


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

    1. from selenium import webdriver
    2. import requests
    3. from bs4 import BeautifulSoup
    4. import os
    5. options = webdriver.ChromeOptions()
    6. options.add_argument('--headless')
    7. # 给请求指定一个请求头来模拟chrome浏览器
    8. headers = {
    9. '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',
    10. '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'
    11. }
    12. server = 'https://www.gequbao.com'
    13. # 凤凰传奇地址
    14. singer = 'https://www.gequbao.com/s/%E8%B4%B9%E7%8E%89%E6%B8%85'
    15. # 获取歌曲内容
    16. def get_contents(song,song_title,singer_name):
    17. # print(song)
    18. save_url = 'G:/python/songs/{}'.format(singer_name)
    19. save_lrc_path = 'G:/python/songs/{}/{}.lrc'.format(singer_name,song_title)
    20. res = requests.get(url=song, headers=headers)
    21. res.encoding = 'utf-8'
    22. html = res.text
    23. soup = BeautifulSoup(html, 'html.parser')
    24. # 获取歌曲的下载链接
    25. driver = webdriver.Chrome(options=options)
    26. driver.get(song)
    27. song_elem = driver.find_element_by_id("btn-download-mp3")
    28. lrc_elem = driver.find_element_by_id("btn-download-lrc")
    29. download_url = song_elem.get_attribute('href')
    30. lrc_url = lrc_elem.get_attribute('href')
    31. # 读取MP3资源
    32. req = requests.get(download_url, stream=True)
    33. # 文件夹不存在,则创建文件夹
    34. folder = os.path.exists(save_url)
    35. if not folder:
    36. os.makedirs(save_url)
    37. # 文件存储地址
    38. full_title = song_title + '.mp3'
    39. file_path = os.path.join(save_url, full_title)
    40. print('开始写入歌曲:', file_path)
    41. # 打开本地文件夹路径file_path,以二进制流方式写入,保存到本地
    42. with open(file_path, 'wb') as fd:
    43. for chunk in req.iter_content():
    44. fd.write(chunk)
    45. print(song_title + '成功下载!')
    46. # 下载歌词
    47. print('开始写入歌词:', save_lrc_path)
    48. r = requests.get(lrc_url).content
    49. with open(save_lrc_path, 'wb') as fd:
    50. fd.write(r)
    51. fd.close()
    52. print(song_title + '歌词成功下载!')
    53. # 主方法
    54. def main():
    55. res = requests.get(singer, headers=headers)
    56. res.encoding = 'utf-8'
    57. html = res.text
    58. # 使用自带的html.parser解析
    59. soup = BeautifulSoup(html, 'html.parser')
    60. # 获取歌曲的列表
    61. songs = soup.find('div', class_='card-text').find_all(class_='text-primary')
    62. singer_name = soup.find('input', id='s-input-line')['value']
    63. print('查询到歌曲数: %d ' % len(songs))
    64. for each in songs:
    65. try:
    66. song = server + each.get('href')
    67. song_title = each.get_text().strip()
    68. get_contents(song,song_title,singer_name)
    69. except Exception as e:
    70. print(e)
    71. if __name__ == '__main__':
    72. main()

    运行之后需要输入要下载的歌手搜索结果页
    下载速度有点慢,而且访问太频繁系统会返回443,但是满足我的需求了,所以也没继续优化,瞎玩呗

  • 相关阅读:
    zynq7000 从github拉取源码进行编译,运行. 快速进行外设验证
    UNIAPP实战项目笔记44 订单页面顶部选项卡 有数据页面样式布局和无数据页面样式布局
    redis我记不住的那些命令(八)
    【待更新】【Rockchip】瑞芯微/rockchip 开发环境搭建|编译|烧录 开发实例
    vue自动触发点击
    机器视觉中的工业光源选择技巧
    用VS Code搞Qt6:编译源代码与基本配置
    AIGC绘本——海马搬家来喽
    什么是边缘计算网关?
    Java IO流实现文件复制
  • 原文地址:https://blog.csdn.net/leavemyleave/article/details/134406274