美文网首页
如今音乐是每个人生活中的调味剂,利用python采集数据,让音乐

如今音乐是每个人生活中的调味剂,利用python采集数据,让音乐

作者: 颜狗一只 | 来源:发表于2022-04-29 14:57 被阅读0次

    知识点:

    • 爬虫基本流程
    • requests的使用
    • hashlib的使用

    环境]:

    • python 3.8
    • pycharm2021.2

    代码

    import requests
    import time
    import hashlib
    
    
    headers = {
        'cookie': 'Hm_lvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650954808; cuid=38cef486-7437-d12b-8266-e9f1af79fc91; Hm_lpvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650958214',
        'from': 'web',
        'referer': 'https://music.91q.com/player',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
    }
    search_timestamp = int(time.time())
    word = '薛之谦'
    appid = '16073360'
    search_r = f"appid={appid}&pageNo=1&pageSize=20&timestamp={search_timestamp}&type=1&word={word}0b50b02fd0d73a9c4c8c3a781c30845f"
    search_sign = hashlib.md5(search_r.encode('utf-8')).hexdigest()
    search_url = 'https://music.91q.com/v1/search'
    search_params = {
        'sign': search_sign,
        'word': word,
        'type': '1',
        'pageNo': '1',
        'pageSize': '20',
        'appid': appid,
        'timestamp': search_timestamp
    }
    search_resp = requests.get(url=search_url, headers=headers, params=search_params)
    music_list = search_resp.json()['data']['typeTrack']
    for music in music_list:
        TSID = music['id']
        url = 'https://music.91q.com/v1/song/tracklink'
        timestamp = int(time.time())
        r = f"TSID={TSID}&appid={appid}&timestamp={timestamp}0b50b02fd0d73a9c4c8c3a781c30845f"
        sign = hashlib.md5(r.encode('utf-8')).hexdigest()
        params = {
            'sign': sign,
            'appid': appid,
            'TSID': TSID,
            'timestamp': timestamp
        }
    # 更多资源、源码、解答可加:832157862
        headers = {
            'cookie': 'Hm_lvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650954808; cuid=38cef486-7437-d12b-8266-e9f1af79fc91; Hm_lpvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650958214',
            'from': 'web',
            'referer': 'https://music.91q.com/player',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
        }
        response = requests.get(url=url, params=params, headers=headers)
        music_url = response.json()['data']['path']
        music_name = response.json()['data']['title']
        singer_name = response.json()['data']['artist'][0]['name']
        print(singer_name, music_name, music_url)
        music_data = requests.get(music_url).content
        with open(f'mp3/{music_name}-{singer_name}-{TSID}.mp3', mode='wb') as f:
            f.write(music_data)
    
    

    相关文章

      网友评论

          本文标题:如今音乐是每个人生活中的调味剂,利用python采集数据,让音乐

          本文链接:https://www.haomeiwen.com/subject/tvesyrtx.html