当前位置: 首页 > news >正文

歌曲爬虫下载

本次编写一个程序要爬取歌曲音乐榜https://www.onenzb.com/ 里面歌曲。有帮到铁子的可以收藏和关注起来!!!废话不多说直接上代码。

1 必要的包

import requests
from lxml import html,etree
from bs4 import BeautifulSoup
import re
import pandas as pd

2 获取歌曲url和歌曲名称

url = 'https://www.onenzb.com/'    
header = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
response = requests.get(url=url, headers=header)
soup = BeautifulSoup(response.text, 'html.parser')  
print(soup)
url_list = []
song_name = []
for link in soup.find_all('a', href=lambda x: x and x.startswith('/music/')):# 提取href属性和title属性href = link.get('href')title = link.get('title')url_ = 'https://www.1nzb.com' + href   # 完整的urlurl_list.append(str(url_))song_name.append(str(title))
song_name = [song_name.replace('/','').replace('CV','').replace('砂狼白子(:安雪璃)早濑优香(:小敢)','') for song_name in song_name]
print(song_name)
print(url_list)

3 解析每首歌曲的url 以及歌名添加

for url,name in dict(zip(url_list,song_name)).items():print(url,name)print(name)header = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'}response = requests.get(url=url, headers=header)soup = BeautifulSoup(response.text, 'html.parser')  # html.parser  lxmlmp3_links = [a['href'] for a in soup.find_all('a', href=True) if a['href'].endswith('.mp3')]# 输出找到的URLfor url in mp3_links:print(url)# MP3文件的URLmp3_url = url# 定义要保存的文件名filename = 'E:/学习/项目/歌曲爬虫/歌曲2/{}.mp3'.format(name)# 发送GET请求response = requests.get(mp3_url, stream=True)# 确保请求成功response.raise_for_status()# 写入文件with open(filename, 'wb') as f:for chunk in response.iter_content(chunk_size=8192):f.write(chunk)print('MP3文件已下载并保存为:', filename)

部分结果:

在这里插入图片描述

http://www.lryc.cn/news/425885.html

相关文章:

  • transformer-explainer
  • C#中的S7协议
  • 2024-08-16升级记录:使用Android RecyclerView控件显示列表型信息
  • 通义千问 ( 一 ) 基础实例
  • docker 修改数据目录
  • r4s软路由写入iStoreOS镜像
  • [C++][opencv]基于opencv实现photoshop算法灰度化图像
  • Emacs23.x版本之重要特性及用法实例(一百五十六)
  • 机器学习 第11章-特征选择与稀疏学习
  • Grok 2携AI图片生成重生
  • 使用Nexus搭建Maven私服仓库
  • 云计算day27
  • 关于HTTP HEAD介绍
  • WPF Mvvm
  • pnpm【实用教程】2024最新版
  • C#的前沿技术有哪些?
  • Vue2移动端(H5项目)项目基于vant封装图片上传组件(支持批量上传、单个上传、回显、删除、预览、最大上传数等功能)---解决批量上传问题
  • ELK整合实战,filebeat和logstash采集SpringBoot项目日志发送至ES
  • 网络编程:OSI协议,TCP/IP协议,IP地址,UDP编程
  • QtExa001自动包装流水线的框架设计vs2019QT
  • SpringBoot拦截器的使用介绍
  • Spring Boot应用中的资源分离与高效打包实践
  • 分析 avformat_open_input 数据读取过程
  • Apache HOP (Hop Orchestration Platform) VS Data Integration (通常被称为 Kettle)
  • 如何判断一个dll/exe是32位还是64位
  • 加速网页加载,提升用户体验:HTML、JS 和 Vue 项目优化全攻略
  • LVS服务器基础环境配置
  • 【Python OpenCV】使用OpenCV实现两张图片拼接
  • springboot jar -jar centos后台运行的几种方式
  • 【GitLab】使用 Docker 安装 GitLab:配置 SSH 端口