吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6062|回复: 40
收起左侧

[Python 转载] 酷我音乐歌曲爬取

  [复制链接]
封心棒棒糖 发表于 2022-5-13 22:01
[Python] 纯文本查看 复制代码
import asyncio
from 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[line:%(lineno)d] - %(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("满天星辰不及你"))

免费评分

参与人数 9吾爱币 +8 热心值 +7 收起 理由
freeflyer + 1 用心讨论,共获提升!
ookk + 1 + 1 谢谢@Thanks!
番茄花园 + 1 谢谢你帮我下载西瓜视频 谢谢
aabbcc123123 + 1 + 1 用心讨论,共获提升!
YoYoYoKing + 1 + 1 谢谢@Thanks!
ymhld + 1 + 1 我很赞同!
scAnn + 1 + 1 谢谢@Thanks!
tutou + 1 + 1 热心回复!
lspzaici + 1 好活当赏

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wangnike 发表于 2022-8-11 16:22
我用requests 死活不行、https://www.kuwo.cn/api/www/search/searchMusicBykeyWord 拿不到json数据、不知道咋了、我请求头都设置好了的,有无大佬帮我看看

[Python] 纯文本查看 复制代码
import urllib.parse
import requests
import Fast_Proxies


def main(ip_list):
    # 代{过}{滤}理次数
    proxies_num = 0
    while True:
        # 设置代{过}{滤}理
        proxies = {
            # "http": 'http://{}'.format(ip_list[proxies_num])
        }
        # 显示使用的代{过}{滤}理
        # 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:29
wangnike 发表于 2022-8-11 16:22
我用requests 死活不行、https://www.kuwo.cn/api/www/search/searchMusicBykeyWord 拿不到json数据、不知 ...

找到问题了、气死我了   
是我这个请求地址写错了

[Python] 纯文本查看 复制代码
                # 拼接完整的请求地址
                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)
YoYoYoKing 发表于 2022-5-13 22:47
大佬,显示line 57
    loop = asyncio.get_event_loop()
DeprecationWarning: There is no current event loop

后面都是红色的歌曲信息列表和id,要怎么下载呢,谢谢了
雾都孤尔 发表于 2022-5-13 23:11
这个支持无损格式的下载吗?
0adsl 发表于 2022-5-13 23:52
要是有酷狗的就棒了
bennyt 发表于 2022-5-14 00:49
音乐爱好者来用用看。
scAnn 发表于 2022-5-14 01:18
太强了,学习一下
 楼主| 封心棒棒糖 发表于 2022-5-14 05:55
YoYoYoKing 发表于 2022-5-13 22:47
大佬,显示line 57
    loop = asyncio.get_event_loop()
DeprecationWarning: There is no current even ...

用python 3.9,或者详细报错截图我看下
无法无天2020 发表于 2022-5-14 06:58
专业性太强,菜鸟路过
头像被屏蔽
tl;dr 发表于 2022-5-14 07:21
提示: 作者被禁止或删除 内容自动屏蔽
XiaoZouYu 发表于 2022-5-14 07:56
菜鸟过来跟着大佬学习下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 05:02

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表