Python爬虫之爬取某音乐平台(付费)歌曲
Python爬虫之爬取某音乐平台(付费)歌曲

Python爬虫之爬取某音乐平台(付费)歌曲

首先新建一个文件夹作为下载路径,然后在创建一个文本,打开输入以下代码:

-- codeing=utf-8 --

@Time:2022/5/12 16:30

@Atuhor:@Benson

@File:批量下载.py

@Software:Benson

导入框架(库,模块) pip install xxxx

import requests
from lxml import etree

http://music.163.com/song/media/outer/url?id=

1、确定网址 真实地址在Network----Doc

url = 'https://music.163.com/playlist?id=2237551001'
base_url = 'http://music.163.com/song/media/outer/url?id='

2、请求(requests) 图片,视频,音频 content 字符串 text

html_str = requests.get(url).text

print(type(html_str)) # 字符串类型

3、筛选数据xpath(标签语言)

//a[contains(@href,'/song?')]/@href

result = etree.HTML(html_str) # 转换类型

print(type(result))

song_ids = result.xpath('//a[contains(@href,"/song?")]/@href') # 歌曲id
song_names = result.xpath('//a[contains(@href,"/song?")]/text()') # 歌名

print(song_ids)

print(song_names) #列表

对列表进行解压

i = 0 # 按顺序来
for song_id,song_name in zip(song_ids,song_names):
# print(song_id)
# print(song_name)
count_id = song_id.strip('/song?id=') # 去掉/song?id=
# print(count_id)

# 过滤含有“$”符号
if ('$' in count_id) == False:
    # print(count_id)
    song_url = base_url + count_id      # 拼接url
    # print(song_url)

    i += 1

    mp3 = requests.get(song_url).content

    # 4、保存数据
    with open('./音乐/{}.{}.mp3'.format(i,song_name),'wb') as file:
        file.write(mp3)

保存关闭,再把文本格式更改成py格式。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Title - Artist
0:00