Python – 爬取酷狗音乐

演示视频

{dplayer src=”http://blog.uurr.cn/video/python_kugou.MP4″/}

示例代码


import requests
import json
from playsound import playsound

print('欢迎来到点歌台')

xh=True

while(xh):

    name = input('请输入您需要的点歌的歌名:\n')

    if name=='':
        xh=False
    else:
        # 伪装

        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.178.400 QQBrowser/11.2.5170.400'
        }

        # url
        lb_url = f"https://mobiles.kugou.com/api/v3/search/song?format=jsonp&keyword={name}"

        r = requests.get(lb_url, headers=headers)

        # get_cookies = r.cookies

        # print(get_cookies)

        list_song = json.loads(r.text[1:-1])['data']['info']

        # .text 表示返回的数据是文本, .content 表示访问的数据是多媒体(如图片,MP3) .json 则表示返回的内容为json

        # print(list_song)

        for i, s in enumerate(list_song):
            result = f"{i + 1} -- {s.get('songname')} -- {s.get('singername')}"
            print(result)

        num = input('请输入您需要点歌的音乐序号:\n')
        # num = 1
        hash = list_song[int(num) - 1].get('hash')
        album_id = list_song[int(num) - 1].get('album_id')

        mp3_url = f"https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={hash}&album_id={album_id}"

        # print(mp3_url)

        get_cookies = 'kg_mid=a3001cca17c3b3b76e49b97da09132d4; kg_dfid=0R8HC83OGrFC3sNpUV0THgTY; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1667128131; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1667137339'

        headers2 = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.178.400 QQBrowser/11.2.5170.400'
            ,
            'Cookie': f'{get_cookies}'

        }
        a = requests.get(mp3_url, headers=headers2)

        # print(a.json())

        mp3 = a.json()['data']['play_url']

        # print(mp3)

        b = requests.get(mp3)

        with open("0.mp3", "wb") as f:
            f.write(b.content)

        print(
            f'正在为您播放 -- {list_song[int(num) - 1].get("songname")} - {list_song[int(num) - 1].get("singername")} ...')

        playsound('D:\My_data\pyproject/0.mp3')

        print("本次播放完成~ 您可以直接回车结束运行!或继续点歌~")
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

夸夸
夸夸
还有吗!没看够!
取消
昵称表情代码图片

    暂无评论内容