Python 爬蟲QQ音樂

HuangZhang_123發表於2017-09-04

Python:3.5
歡迎加入學習交流QQ群:657341423


爬取高質量mp3

import requests
headers={'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Cache-Control':'no-cache','Connection':'keep-alive','Host':'dl.stream.qqmusic.qq.com','Pragma':'no-cache','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2717.400 QQBrowser/9.6.11133.400'}

url='https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?g_tk=1400579671&jsonpCallback=MusicJsonCallback1725281637681917&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&cid=205361747&callback=MusicJsonCallback1725281637681917&uin=0&songmid=003nzgAn0jKPGF&filename=C400003nzgAn0jKPGF.m4a&guid=7286222600'
r=requests.get(url,headers=headers)
print (r.text)
text=r.text.split('(')[1].split(')')[0]
import json
get_json=json.loads(text)
a=get_json['data']['items'][0]['vkey']
print(a)
url='http://dl.stream.qqmusic.qq.com/C400003nzgAn0jKPGF.m4a?vkey=%s&guid=7286222600&uin=0&fromtag=66' %(a)
r=requests.get(url,headers=headers)

f=open('data.m4a','wb')
f.write(r.content)
f.close()
print(r.status_code)

引數修改:第一個url是songmid=003nzgAn0jKPGF,filename=C400003nzgAn0jKPGF.m4a
第二個引數是C400003nzgAn0jKPGF.m4a 和vkey

值得注意的是:第一個連結loginUin,hostUin和uin要等於第二個uin,
兩者guid要相等

獲取歌單資訊

import requests

#獲取全部分類歌單的categoryId
#https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_tag_conf.fcg?g_tk=5381&jsonpCallback=getPlaylistTags&loginUin=0&
# hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0

#國語歌單的歌單列表資訊
#https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?rnd=0.07499648392820268&g_tk=5381&jsonpCallback=getPlaylist&
# loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&categoryId=165&sortId=5&sin=0&ein=29

#熱門歌單的歌單列表資訊
url= 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?rnd=0.04378599143046411&g_tk=5381&jsonpCallback=getPlaylist&' \
     'loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&categoryId=10000000&sortId=5&sin=0&ein=29'

#categoryId為分類標籤
#sin為開始,ein為結束,這url代表第一頁,30~59代表第二頁
headers={'referer':'https://y.qq.com/portal/playlist.html',
         'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}

r = requests.get(url,headers=headers)
print (r.text)

相關文章