python爬取网易云音乐专辑的所有歌曲

爬取过程分析:
1、确定要爬取专辑的url实际地址
在这里插入图片描述
2、通过xpath筛选数据,找出该专辑所有歌曲的id
在这里插入图片描述
3、将歌曲的id与外链地址相拼接,得到歌曲的真是下载地址
网易云音乐外链地址:https://link.hhtjim.com/
4、下载,将歌曲写入文件

代码如下:

# import requests
#
# url = 'https://m10.music.126.net/20190730085044/771b0fa0b18f4ff8a665512d4b868b93/yyaac/545e/065b/0e52/c4653bbfee11db0fa6039818fe9869d9.m4a'
#
# results = requests.get(url).content
# with open('./hhh.m4a','wb') as f:
#     f.write(results)


import requests
from lxml import etree
# 确定url地址
url = 'https://music.163.com/artist?id=44266'
base_url = 'https://link.hhtjim.com/163/'

# 请求
results = requests.get(url).text  #以文本方式显示
# print(results)

# 筛选数据
dom = etree.HTML(results)
ids = dom.xpath('//a[contains(@href,"song")]/@href')
# print(ids)

for song_id in ids:
    #过滤切割
    count_id = song_id.strip('/song?id=')
    # 过滤$符号
    if('$' in count_id )== False:
        # print(count_id)
        song_url = base_url + '%s'%count_id+'.mp3'
        # print(song_url)
        song_name = song_url.split('/')[-1]#切割url,以歌的id命名
        print(song_name)
        music = requests.get(song_url).content
        # 写入文件
        with open('./%s'%song_name,'wb') as file:
            file.write(music)
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值