酷我音乐歌曲爬取
import asynciofrom urllib.parse import quote
import aiohttp
import logging
import aiofiles
referer = 'https://www.kuwo.cn'
# 请求头
headers = {
"Cookie": "_ga=GA1.2.2021007609.1602479334; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1602479334,1602673632; "
"_gid=GA1.2.168402150.1602673633; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1602673824; "
"kw_token=5LER5W4ZD1C",
"csrf": "5LER5W4ZD1C",
"Referer": "{}".format(referer),
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/86.0.4240.75 Safari/537.36",
}
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(filename)s - %(levelname)s: %(message)s')
async def searchSong(session, song_name):
encodeName = quote(song_name)
url = f'https://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={encodeName}&pn=1&rn=30&httpsStatus=1'
res = await session.get(url=url, headers=headers)
return await res.json()
async def downloadSong(session, rid, name, singer):
url = f'http://www.kuwo.cn/api/v1/www/music/playUrl?mid={rid}&type=convert_url3&br=320kmp3'
res = await session.get(url=url, headers=headers)
res_json = await res.json()
download_url = res_json['data']['url']
response = await session.get(download_url, headers=headers)
data = await response.content.read()
async with aiofiles.open(f'music/{name}-{singer}.mp3', mode='wb') as f:
logging.info(f"{name}-{singer}.mp3下载完成")
await f.write(data)
async def main(song_name):
async with aiohttp.ClientSession() as session:
song_json = await searchSong(session, song_name)
songs_info = song_json['data']['list']
tasks = []
for song_info in songs_info:
name = song_info['name'].replace(' ', '').replace('へ-', '').replace('|', '')
artist = song_info['artist']
rid = song_info['rid']
logging.info(f"歌曲名称:{name}|歌手:{artist}|id:{rid}")
tasks.append(asyncio.create_task(downloadSong(session, rid, name, artist)))
await asyncio.wait(tasks)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main("满天星辰不及你"))
我用requests 死活不行、https://www.kuwo.cn/api/www/search/searchMusicBykeyWord 拿不到json数据、不知道咋了、我请求头都设置好了的,有无大佬帮我看看
import urllib.parse
import requests
import Fast_Proxies
def main(ip_list):
# 代{过}{滤}理次数
proxies_num = 0
while True:
# 设置代{过}{滤}理
proxies = {
# "http": 'http://{}'.format(ip_list)
}
# 显示使用的代{过}{滤}理
# print('代{过}{滤}理地址: {}, 代{过}{滤}理次数{}'.format(proxies["http"], proxies_num))
# 目标地址
website = 'https://www.kuwo.cn'
# 请求头协议
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36",
"Cookie": "_ga=GA1.2.2044940001.1660029306; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1660029306,1660180501; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1660180501; _gid=GA1.2.969805937.1660180501; _gat=1; kw_token=I9OHC3BJSC",
# "Accept": "application/json, text/plain, */*",
# "Accept-Encoding": "gzip, deflate",
# "Accept-Language": "zh-CN, zh;q=0.9",
# "Connection": "keep-alive",
"Referer": "http://www.kuwo.cn/search/list?key=%E5%BF%98%E8%AE%B0%E6%97%B6%E9%97%B4",
"csrf": "I9OHC3BJSC"
}
try:
while True:
# music_name_input = input("请输入音乐名称: ")
music_name_input = "忘记时间"
# 对音乐名称进行编码成ASCII字符
music_name_quote = urllib.parse.quote(music_name_input)
# 拼接完整的请求地址
search_music_url = '{}/api/www/search/searchMusicBykeyWord?key={}&pn=1&rn=30&httpsStatus=1'.format(website, music_name_quote)
print(search_music_url)
# 发起get请求
response = requests.get(website, headers=header, proxies=proxies)
# # 设置网页响应的编码格式
response_encoding = response.apparent_encoding
print(response.text)
break
except (requests.exceptions.ProxyError, requests.exceptions.ChunkedEncodingError):
proxies_num += 1
else:
break
if __name__ == '__main__':
main(Fast_Proxies.get_proxies_ip())
wangnike 发表于 2022-8-11 16:22
我用requests 死活不行、https://www.kuwo.cn/api/www/search/searchMusicBykeyWord 拿不到json数据、不知 ...
找到问题了、气死我了 :wwqwq
是我这个请求地址写错了
# 拼接完整的请求地址
search_music_url = '{}/api/www/search/searchMusicBykeyWord?key={}&pn=1&rn=30&httpsStatus=1'.format(website, music_name_quote)
print(search_music_url)
# 发起get请求
response = requests.get(search_music_url, headers=header, proxies=proxies) 大佬,显示line 57
loop = asyncio.get_event_loop()
DeprecationWarning: There is no current event loop
后面都是红色的歌曲信息列表和id,要怎么下载呢,谢谢了 这个支持无损格式的下载吗? 要是有酷狗的就棒了 音乐爱好者来用用看。 太强了,学习一下 YoYoYoKing 发表于 2022-5-13 22:47
大佬,显示line 57
loop = asyncio.get_event_loop()
DeprecationWarning: There is no current even ...
用python 3.9,或者详细报错截图我看下 专业性太强,菜鸟路过 菜鸟过来跟着大佬学习下