python爬虫实战——获取酷我音乐数据

嗨喽,大家好呀~这里是爱看美女的茜茜呐

python爬虫实战——获取酷我音乐数据_第1张图片

开发环境:

  • 版 本: python 3.8

  • 编辑器:pycharm 2022.3.2

模块使用:

  • requests >>> pip install requests

如何安装python第三方模块:

  1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

  2. 在pycharm中点击Terminal(终端) 输入安装命令


更多精彩机密、教程,尽在下方,赶紧点击了解吧~

python源码、视频教程、插件安装教程、资料我都准备好了,直接在文末名片自取就可


代码实现步骤:

  1. 发送请求 -> 模拟浏览器对于url地址发送请求

  2. 获取数据

  3. 解析数据

  4. 保存数据

代码展示

导入模块

import requests
def get_music(music_name, mid):
    print(music_name, mid)
    try:

模拟伪装

'''
python资料获取看这里噢!! 小编 V:Pytho8987(记得好友验证备注:6  笔芯~)
即可获取:文章源码/教程/资料/解答等福利,还有不错的视频学习教程和PDF电子书!
'''
        headers = {
            'Cookie': '_ga=GA1.2.1106834021.1675858131; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1706337476; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1706337476; _gid=GA1.2.2033741782.1706337476; _ga_ETPBRPM9ML=GS1.2.1706337476.11.1.1706337507.29.0.0; Hm_Iuvt_cdb524f42f0cer9b268e4v7y735ewrq2324=t6et7mhEk5GBt7ths6txPMFxaQYBiN56',
            'Host': 'www.kuwo.cn',
            'Referer': 'https://www.kuwo.cn/play_detail/66260268',
            'Secret': 'f7e2963072c1bfa88db91bd9a6e85241ad92e8aa17508ccaa1598c4fcd07565904a350a2',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        }
  1. 发送请求
        url = f'https://www.kuwo.cn/api/v1/www/music/playUrl?mid={mid}'
        response = requests.get(url=url, headers=headers)
  1. 获取数据 提取数据
        music_url = response.json()['data']['url']
        music_data = requests.get(music_url).content
        with open(f'music/{music_name}.mp3', mode='wb') as f:
            f.write(music_data)
    except:
        print(f'下载异常: {music_name} 请更换vip账号cookie之后再尝试')
def get_music_info(name, count):
    headers = {
        'Referer': 'https://www.kuwo.cn/search/list?key=%E6%9D%8E%E8%8D%A3%E6%B5%A9',
        'Cookie': '_ga=GA1.2.1106834021.1675858131; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1706337476; _gid=GA1.2.2033741782.1706337476; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1706337566; _gat=1; _ga_ETPBRPM9ML=GS1.2.1706337476.11.1.1706338270.47.0.0; Hm_Iuvt_cdb524f42f0cer9b268e4v7y735ewrq2324=intpHfGWinsD6rxfjENe8NwbMPAfQzTG',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    }
    url = f'https://www.kuwo.cn/search/searchMusicBykeyWord?vipver=1&client=kt&ft=music&cluster=0&strategy=2012&encoding=utf8&rformat=json&mobi=1&issubtitle=1&show_copyright_off=1&pn=0&rn={count}&all={name}'
    resp = requests.get(url, headers=headers)
    json_data = resp.json()
    abslist = json_data['abslist']
    info_list = []
    for abs in abslist:
        song_name = abs['SONGNAME']
        song_id = abs['DC_TARGETID']
        info_list.append([song_id, song_name])
    return info_list


info_list = get_music_info('李荣浩', 100)
for info in info_list:
    mid = info[0]
    music_name = info[1]
    get_music(music_name, mid)

尾语

感谢你观看我的文章呐~本次航班到这里就结束啦

希望本篇文章有对你带来帮助 ,有学习到一点知识~

躲起来的星星也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,宣传一下呀~更多源码、资料、素材、解答、交流皆点击下方名片获取呀

你可能感兴趣的:(python爬虫,python,爬虫,开发语言,pycharm,学习)