歌曲爬虫下载

本次编写一个程序要爬取歌曲音乐榜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   # 完整的url
    url_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  lxml
    mp3_links = [a['href'] for a in soup.find_all('a', href=True) if a['href'].endswith('.mp3')]
    # 输出找到的URL
    for url in mp3_links:
        print(url)
        # MP3文件的URL
        mp3_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)

部分结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值