注意:不建议学习bs4,懂的都懂
今天咱们就搞定它
# *改成k
url='https://www.*uwo.cn/'
import requests
from urllib import parse
import prettytable as pt
headers = {
# 浏览器基本信息
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
# 辨别用户的身份
'Cookie': '*',
# 认证令牌
'csrf': 'C713RK6IJ8J',
# 指定的请求资源的域名
# *改成k
'Host': 'www.*uwo.cn',
# 用来跟踪Web请求来自哪个页面,是从什么网站来的。
'Referer': 'http://www.*uwo.cn/search/list?key='+searchKey
}
# *改成k
url = f'http://www.*uwo.cn/api/www/search/searchMusicBykeyWord?key={searchKey}&pn=1&rn=30'
json_data = requests.get(url=url, headers=headers).json()
song_list = json_data['data']['list']
count = 0
info_list = []
tb = pt.PrettyTable()
tb.field_names = ['序号', '歌名', '歌手', '专辑']
for song in song_list:
singer_name = song['artist']
song_name = song['name']
album_name = song['album']
rid = song['rid']
info_list.append([rid, song_name, singer_name])
tb.add_row([count, song_name, singer_name, album_name])
# *改成k
song_info_url = f'https://www.*uwo.cn/api/v1/www/music/playUrl?mid={rid}&type=convert_url3&br=320kmp3'
music_url = requests.get(song_info_url, headers=headers).json()['data']['url']
music_data = requests.get(music_url).content
with open(f'download/{singer_name}-{song_name}.mp3', mode='wb') as f:
f.write(music_data)
print(f'{song_name}', '下载完成!!!')
count += 1
print(tb)
input_index = eval(input("请输入要下载歌曲的序号(-1退出): "))
if input_index == -1:
break
download_info = info_list[input_index]
# 流畅音质 128k
# 高频音质 192k
# 超品音质 320k
# *改成k
song_info_url = f'https://www.*uwo.cn/api/v1/www/music/playUrl?mid={download_info[0]}&type=convert_url3&br=320kmp3'
music_url = requests.get(song_info_url, headers=headers).json()['data']['url']
music_data = requests.get(music_url).content
with open(f'download/{download_info[1]}-{download_info[2]}.mp3', mode='wb') as f:
f.write(music_data)
print(f'{download_info[1]}', '下载完成!!!')
学会了嘛?需要源码可以留言、私信我。
结尾给大家推荐一个非常好的学习教程,希望对你学习Python有帮助!
Python基础入门教程推荐:←点击左边蓝色文字就可以跳转观看了
Python爬虫案例教程推荐:←点击左边蓝色文字就可以跳转观看了