python 爬虫小例子4-酷狗音乐

import requests
import json,os,re
##获取url和headers
url='https://www.kugou.com/yy/html/rank.html'
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46',
        'cookie': 'kg_mid=00002f72f1f5cf034d4be17546749f41; kg_dfid=4DWhsN0oogyN14bJmn3bFtt7; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1619358356; kg_mid_temp=00002f72f1f5cf034d4be17546749f41; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1619358689',
         'referer': 'https://www.kugou.com/song'
         }
html=requests.get(url)


####匹配hash 所在的字典
re_hash=re.compile('{"hash":"(.*?)"}',re.S|re.I)
hashs_ids=re_hash.findall(html.text)
#print(hashs)

####遍历hash
for h in hashs_ids:
    
    url='https://wwwapi.kugou.com/yy/index.php'
    new_h=json.loads('{"hash":"'+h+'"}')
    params={
            'r': 'play/getdata',
            'callback': 'jQuery19109236575204602362_1607073103172',
            'hash': new_h['hash'],
            'dfid': '4DWhsN0oogyN14bJmn3bFtt7',
            'album_id':new_h['album_id']
            }
    
    html=requests.get(url,params=params,headers=headers).text
    
    ###截取返回长度
    start=html.index("{")
    end=html.index("})")+1
    
    #####获取数据
    data=json.loads(html[start:end])['data']
    song_name=data['song_name']
    song_url=data['play_url']
    print(song_name)
    print(song_url)
    
    ###下载歌曲

你可能感兴趣的:(python 爬虫小例子4-酷狗音乐)