首页 > 编程语言 >python学习记录11:网易云音乐批量下载工具爬虫源码

python学习记录11:网易云音乐批量下载工具爬虫源码

时间:2022-10-17 18:35:04浏览次数:69  
标签:11 下载工具 song url ids sult 源码 print id

网易云批量下载工具

import requests
from lxml import etree
import datetime
import time
curr_time=datetime.datetime.now()
times = datetime.datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S')
print(f'        现在是:{times}')
print('*********欢迎使用网易云VIP音乐批量下载工具**********')
url_source=input('请输入网易云音乐列表地址(url):')
url=url_source
domain='http://music.163.com/song/media/outer/url?id='
html_str = requests.get(url).text
#print(type(html_str))
result = etree.HTML(html_str)

#print(type(result))
#song_ids = result.xpath('//a[contains(@href,"/song?")]/@href')
song_ids = result.xpath('//a[contains(@href,"/song?")]/@href')   # 歌曲id
song_names = result.xpath('//a[contains(@href,"/song?")]/text()')       # 歌名
count=len(song_ids)
print('该列表共有{}首歌曲,开始下载.......'.format(str(count)))
time.sleep(3)
#print(song_ids)
i=0
for song_id,song_name in zip(song_ids,song_names):
    #print(song_id)
    #print(song_name)
    sult_id=song_id.strip('/song?id=')
    #print(sult_id)
    #resulturl=domain+sult_id
    #print(resulturl)
    if ('$' in sult_id) == False:
#        # print(count_id)
        song_url = domain + sult_id      # 拼接url
        #print(song_url)
    i += 1
    mp3 = requests.get(song_url).content
    with open('新歌/{}.{}.mp3'.format(i, song_name), 'wb') as file:
        file.write(mp3)
        print('歌曲{}{}.mp3下载成功!'.format(i,song_name))

标签:11,下载工具,song,url,ids,sult,源码,print,id
From: https://www.cnblogs.com/qlsh/p/16800175.html

相关文章