功能展示
# _*_ coding:utf-8 _*_ # 开发时间:2022/11/26 15:43 # 开发工具:PyCharm import urllib.parse from urllib.request import urlopen import json import time import sys import os def Time_1(): for i in range(1, 51): sys.stdout.write('\r') sys.stdout.write('{0}% |{1}'.format(int(i % 51) * 2, int(i % 51) * '■')) sys.stdout.flush() time.sleep(0.125) sys.stdout.write('\n') def KuGou_music(): keyword = urllib.parse.urlencode({'keyword': input('请输入歌名:')}) keyword = keyword[keyword.find('=') + 1:] url = 'https://songsearch.kugou.com/song_search_v2?callback=jQuery1124042761514747027074_1580194546707&keyword=' + keyword + '&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1580194546709' content = urlopen(url=url) content = content.read().decode('utf-8') str_1 = content[content.find('(') + 1:-2] str_2 = json.loads(str_1) Music_Hash = {} Music_id = {} for dict_1 in str_2['data']['lists']: Music_Hash[dict_1['FileName']] = dict_1['FileHash'] Music_id[dict_1['FileName']] = dict_1['AlbumID'] list_music_1 = [music for music in Music_Hash] list_music = [music for music in Music_Hash] for i in range(len(list_music)): if '- <em>' in list_music[i]: list_music[i] = list_music[i].replace('- <em>', '-') if '</em>' in list_music[i]: list_music[i] = list_music[i].replace('</em>', '') if '<em>' in list_music[i]: list_music[i] = list_music[i].replace('<em>', '') for i in range(len(list_music)): print("{}-:{}".format(i + 1, list_music[i])) music_id_1 = int(input('请输入你想下载的歌曲序号:')) url = 'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=' + Music_Hash[ list_music_1[music_id_1 - 1]] + '&album_id=' + Music_id[list_music_1[ music_id_1 - 1]] + '&dfid=2SSV0x4LWcsx0iylej1F6w7P&mid=44328d3dc4bfce21cf2b95cf9e76b968&platid=4' js_content = urlopen(url=url) str_3 = js_content.read().decode('utf-8') dict_2 = json.loads(str_3) try: music_href = dict_2['data']['play_backup_url'] music_content = urlopen(url=music_href).read() try: os.mkdir('D:/爬虫爬取/MP3/') except Exception as e: print(e, '稍等,程序仍然执行') finally: music_path = 'D:/爬虫爬取/MP3/' + list_music[music_id_1 - 1] + '.mp3' # 歌曲下载路径 with open(music_path, 'wb') as f: print('正在下载当中...') f.write(music_content) Time_1() print('{}.mp3下载成功!'.format(list_music[music_id_1 - 1])) except: print('对不起,没有该歌曲的版权!') if __name__ == '__main__': KuGou_music()
网易云vip音乐免费下载方法(python爬虫)
import os
import re
import json
import requests
from lxml import etree
def download_songs(url=None):
if url is None:
url = 'https://music.163.com/#/playlist?id=2384642500'
url = url.replace('/#', '').replace('https', 'http') # 对字符串进行去空格和转协议处理
# 网易云音乐外链url接口:http://music.163.com/song/media/outer/url?id=xxxx
out_link = 'http://music.163.com/song/media/outer/url?id='
# 请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Referer': 'https://music.163.com/',
'Host': 'music.163.com'
}
# 请求页面的源码
res = requests.get(url=url, headers=headers).text
tree = etree.HTML(res)
# 音乐列表
song_list = tree.xpath('//ul[@class="f-hide"]/li/a')
# 如果是歌手页面
artist_name_tree = tree.xpath('//h2[@id="artist-name"]/text()')
artist_name = str(artist_name_tree[0]) if artist_name_tree else None
# 如果是歌单页面:
#song_list_tree = tree.xpath('//*[@id="m-playlist"]/div[1]/div/div/div[2]/div[2]/div/div[1]/table/tbody')
song_list_name_tree = tree.xpath('//h2[contains(@class,"f-ff2")]/text()')
song_list_name = str(song_list_name_tree[0]) if song_list_name_tree else None
# 设置音乐下载的文件夹为歌手名字或歌单名
folder = './' + artist_name if artist_name else './' + song_list_name
if not os.path.exists(folder):
os.mkdir(folder)
for i, s in enumerate(song_list):
href = str(s.xpath('./@href')[0])
song_id = href.split('=')[-1]
src = out_link + song_id # 拼接获取音乐真实的src资源值
title = str(s.xpath('./text()')[0]) # 音乐的名字
filename = title + '.mp3'
filepath = folder + '/' + filename
print('开始下载第{}首音乐:{}\n'.format(i + 1, filename))
try: # 下载音乐
#下载歌词
#download_lyric(title, song_id)
data = requests.get(src).content # 音乐的二进制数据
with open(filepath, 'wb') as f:
f.write(data)
except Exception as e:
print(e)
print('{}首全部歌曲已经下载完毕!'.format(len(song_list)))
def download_lyric(song_name, song_id):
url = 'http://music.163.com/api/song/lyric?id={}&lv=-1&kv=-1&tv=-1'.format(song_id)
# 请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Referer': 'https://music.163.com/',
'Host': 'music.163.com'
# 'Origin': 'https://music.163.com'
}
# 请求页面的源码
res = requests.get(url=url, headers=headers).text
json_obj = json.loads(res)
lyric = json_obj['lrc']['lyric']
reg = re.compile(r'\[.*\]')
lrc_text = re.sub(reg, '', lyric).strip()
print(song_name, lrc_text)
a = 'https://music.163.com/#/playlist?id=7608893879'
if __name__ == '__main__':
# #music_list = 'https://music.163.com/#/playlist?id=2384642500' #歌曲清单
music_list = 'https://music.163.com/#/playlist?id=7743939886' #1
# music_list = 'https://music.163.com/#/playlist?id=7743887033' #2
# music_list = 'https://music.163.com/#/playlist?id=7744228361' #3
# music_list = 'https://music.163.com/#/playlist?id=7744194170' #4
# music_list = 'https://music.163.com/#/playlist?id=7744204867' #5
# music_list = 'https://music.163.com/#/playlist?id=7743971782' #6
# music_list = 'https://music.163.com/#/playlist?id=7744098326' #7
# music_list = 'https://music.163.com/#/playlist?id=7743939892' #8
# music_list = 'https://music.163.com/#/playlist?id=7743891024' #9
# music_list = 'https://music.163.com/#/playlist?id=7744240226' #10 #我的网易云那啥
# music_list = 'https://music.163.com/#/search/m/?order=hot&cat=全部&limit=435&offset=435&s=梁静茹' #搜索列表
download_songs(music_list)
如有侵权请联系删除(忘记了是在哪里抄的了)
vscode VSCodeUserSetup-x64-1.57.0
pip install lxml
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://hqyman.cn/post/6713.html 非本站原创文章欢迎转载,原创文章需保留本站地址!
休息一下~~