Python爬虫入门教程14:喜马拉雅有声书音频爬取

前言💨

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文内容💨

Python爬虫入门教程01:豆瓣Top电影爬取

Python爬虫入门教程02:小说爬取

Python爬虫入门教程03:二手房数据爬取

Python爬虫入门教程04:招聘信息爬取

Python爬虫入门教程05:B站视频弹幕的爬取

Python爬虫入门教程06:爬取数据后的词云图制作

Python爬虫入门教程07:腾讯视频弹幕爬取

Python爬虫入门教程08:爬取csdn文章保存成PDF

Python爬虫入门教程09:多线程爬取表情包图片

Python爬虫入门教程10:彼岸壁纸爬取

Python爬虫入门教程11:新版王者荣耀皮肤图片的爬取

Python爬虫入门教程12:英雄联盟皮肤图片的爬取

Python爬虫入门教程13:高质量电脑桌面壁纸爬取

PS:如有需要 Python学习资料 以及 解答 的小伙伴可以加点击下方链接自行获取
python免费学习资料以及群交流解答点击即可加入

基本开发环境💨

  • Python 3.6
  • Pycharm

相关模块的使用💨

import requests
import os

安装Python并添加到环境变量,pip安装需要的相关模块即可。

一、💥明确目标

爬取免费的播放量最多的有声书,如果你想要爬取付费的也是可以,那你得先开一个会员,
爬虫是看的到才能爬。
在这里插入图片描述

二、💥网页数据分析

点击任意一个音频章节点击播放。在开发者工具中选择 Media 会加载出一个数据
在这里插入图片描述
复制这个链接会自动下载一个可播放的音频文件。在这里插入图片描述
在这里插入图片描述
听内容是和网站上面的有声小说是一样的。所以这个就是音频数据的真实url地址。
接下来就要分析找到这个数据来源。
在这里插入图片描述
在开发者工具中搜索 wKgJJ1eKo-3xGS0KAFIc8J_87NE024 框选的内容就是音频本身,所有查看第一个地址。
在这里插入图片描述

https://www.ximalaya.com/revision/play/v1/audio?id=18556416&ptype=1

这个数据里面包含了音频地址。

其实这个 id=18556416 就是每个音频的ID值了。同样的在开发者工具中进行搜索。

在这里插入图片描述
有声书名字,音频ID,章节名字都有了。但是当我查看第二页数据的时候发现,并不是这个链接,而是另外一个链接。

https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=4756811&pageNum=2

在这里插入图片描述
还是有一些区别的,所以我们应该请求的是第二个链接

😀 整体思路分析

1、通过 'https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=4756811&pageNum=2'
url地址获取每章音频的ID以及章节名字
2、通过 'https://www.ximalaya.com/revision/play/v1/audio?id=18556416&ptype=1' url地址获取
每章音频的下载地址
3、请求音频地址,进行本地保存

三、💥代码实现

1、获取获取每章音频的ID以及章节名字

def get_audio_info(html_url):
    json_data = get_response(html_url).json()
    audio_info = json_data['data']['tracks']
    for index in audio_info:
        # 音频ID
        audio_id = index['trackId']
        # 章节名字
        audio_title = index['title']
        # 有声书小说名字 《摸金天师》第001章 百辟刀
        audio_name = audio_title.split('第')[0]
 

2、获取音频url

def get_audio_url(audio_id):
    page_url = f'https://www.ximalaya.com/revision/play/v1/audio?id={audio_id}&ptype=1'
    json_data = get_response(page_url).json()
    audio_url = json_data['data']['src']
    return audio_url

3、保存音频

def save(name, title, audio_url):
    path = f'{name}\\'
    if not os.path.exists(path):
        os.makedirs(path)
    audio_content = get_response(audio_url).content
    with open(path + title + '.m4a', mode='wb') as f:
        f.write(audio_content)
        print('正在保存:', title)

💥完整实现代码

import requests
import os


def get_response(html_url):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=header)
    return response


def save(name, title, audio_url):
    path = f'{name}\\'
    if not os.path.exists(path):
        os.makedirs(path)
    audio_content = get_response(audio_url).content
    with open(path + title + '.m4a', mode='wb') as f:
        f.write(audio_content)
        print('正在保存:', title)


def get_audio_url(audio_id):
    page_url = f'https://www.ximalaya.com/revision/play/v1/audio?id={audio_id}&ptype=1'
    json_data = get_response(page_url).json()
    audio_url = json_data['data']['src']
    return audio_url


def get_audio_info(html_url):
    json_data = get_response(html_url).json()
    audio_info = json_data['data']['tracks']
    for index in audio_info:
        # 音频ID
        audio_id = index['trackId']
        # 章节名字
        audio_title = index['title']
        # 有声书小说名字 《摸金天师》第001章 百辟刀
        audio_name = audio_title.split('第')[0]
        audio_url = get_audio_url(audio_id)
        save(audio_name, audio_title, audio_url)


if __name__ == '__main__':
    for page in range(1, 39):
        url = f'https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=4756811&pageNum={page}'
        get_audio_info(url)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

posted @ 2021-02-02 15:15  有趣的Python  阅读(1576)  评论(0编辑  收藏  举报