亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

python 爬取QQ音樂

標簽:
Python

import requests

import json

import os

import threading

#发送请求获取信息

def get_response(url):

    headers = {

        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'

    }

    response = requests.get(url=url, headers=headers).content

    return response


#保存文件

def save_mp3(music_name,res,word):

    filePath = os.path.join(os.getcwd(), './mp3/' + word)

    if not os.path.exists(filePath):

        print('路径不存在')

        os.makedirs(filePath)

        print('创建成功')

        with open('./mp3/' + word + "/" + music_name + ".m4a", "wb") as file:

            file.write(res)

    else:

        with open('./mp3/' + word + "/" + music_name + ".m4a", "wb") as file:

            file.write(res)


#发送请求

def send_request():

    word = 'exodus'

    res1 = requests.get(

        'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&lossless=0&flag_qc=0&p=1&n=20&w=' + word)

    jm1 = json.loads(res1.text.strip('callback()[]'))

    list_url = []

    re = jm1['data']['song']['list']

    for j in re:

        try:

            list_url.append((j['songmid'], j['songname'].replace('/',''), j['singer'][0]['name']))

        except:

            print('wrong')

    for i in list_url:

        print("开始下载 %s" %i[1])

        res = get_response("http://ws.stream.qqmusic.qq.com/C100"+i[0]+".m4a?fromtag=0")

        #save_mp3(i[1],res,word)

        thr = threading.Thread(target=save_mp3, args=(i[1],res,word))

        # 启动线程

        thr.start()

        print("下载完成")

    print("done")

send_request()

找到QQ音乐的搜索歌曲API

https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&lossless=0&flag_qc=0&p=1&n=20&w= + keyword

此接口获取搜索的歌曲

http://ws.stream.qqmusic.qq.com/C100"+songid+".m4a?fromtag=0

获取播放接口,通过模拟浏览器打开音乐来下载音乐

使用get_response()来下载

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消