python爬取喜马拉雅有声小说

绝世高手为例,爬取前三页的所有音频
采集地址:https://www.ximalaya.com/youshengshu/16411402/

思路只有两个:获取一级页面、获取二级页面

先一步二步看一下代码:

    def get_mes(self):
        id_list = []
        # 获取前三页的地址,range顾头不顾尾
        for i in range(1, 4):
            page_url = f'https://www.ximalaya.com/youshengshu/16411402/p{i}/'
            # 请求页面
            response = requests.get(url=page_url, headers=self.headers).content.decode('utf-8')
            # 使用正则表达式获取小说名字和id
            # 获取id是获取音频给关键
            pat = '<a title="(.*?)" href="(.*?)">'
            result = re.findall(pat, response, re.S)
            # print(result)
            for title, href in result[6:]:
                self.name_list.append(title)
                id_list.append(href.split('/')[-1])
                # self.url_list = 'https://www.ximalaya.com' + href
        return id_list

如果你觉得两个for看着不舒服也可以拆成两个方法
翻页就是通过观察它的地址:
在这里插入图片描述

    def get_audio(self, id_list):
        j = 0
        for i in id_list:
            audio_url = f'https://www.ximalaya.com/revision/play/v1/audio?id={i}&ptype=1'
            res = requests.get(audio_url, headers=self.headers).content.decode('utf-8')
            pat = '"src":"(.*?)"'
            result = re.findall(pat, res, re.S)
            # print(result[0])
            response = requests.get(result[0])
            if response:
                with open(f'{self.name_list[j]}.m4a', 'wb') as f:
                    f.write(response.content)
                print(f'{self.name_list[j]}爬取成功')
                j += 1

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
完整代码:

import requests
import re


class Spider(object):
    def __init__(self):
        # 放名字
        self.name_list = []
        # self.url_list = []
        # 请求头
        self.headers = {
            'user-agent': '这里是你的user-agent的内容'
        }

    def get_mes(self):
        id_list = []
        for i in range(1, 4):
            page_url = f'https://www.ximalaya.com/youshengshu/16411402/p{i}/'
            response = requests.get(url=page_url, headers=self.headers).content.decode('utf-8')
            pat = '<a title="(.*?)" href="(.*?)">'
            result = re.findall(pat, response, re.S)
            # print(result)
            for title, href in result[6:]:
                self.name_list.append(title)
                id_list.append(href.split('/')[-1])
                # self.url_list = 'https://www.ximalaya.com' + href
        return id_list

    def get_audio(self, id_list):
        j = 0
        for i in id_list:
            audio_url = f'https://www.ximalaya.com/revision/play/v1/audio?id={i}&ptype=1'
            res = requests.get(audio_url, headers=self.headers).content.decode('utf-8')
            pat = '"src":"(.*?)"'
            result = re.findall(pat, res, re.S)
            # print(result[0])
            response = requests.get(result[0])
            if response:
                with open(f'{self.name_list[j]}.m4a', 'wb') as f:
                    f.write(response.content)
                print(f'{self.name_list[j]}爬取成功')
                j += 1

    def run(self):
        id_list = self.get_mes()
        self.get_audio(id_list)


spider = Spider()
spider.run()

代码有不够好的地方,后面可能会以其他的例子作为改进

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python爬取喜马拉雅的数据,可以使用requests库发送HTTP请求获取数据。首先,你需要使用requests.get()方法发送GET请求到喜马拉雅的API接口,例如: response = requests.get('https://www.ximalaya.com/revision/play/album?albumId=16372952&pageNum=1&sort=-1&pageSize=30') 然后,你可以使用json.loads()方法将返回的数据转换成字典对象,以便于处理和提取其中的信息。例如: data = json.loads(response.text) audio_data = data['data']['tracksAudioPlay'] 这样你就可以获取到喜马拉雅的音频数据。接下来,你可以根据需要使用这些音频数据进行相关的分析、存储或其他操作。注意,这里的示例只是一个基本的示范,具体的爬取操作可能需要根据喜马拉雅的API文档和需求进行进一步的定制和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python爬虫|爬取喜马拉雅音频](https://blog.csdn.net/weixin_34122548/article/details/93600299)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python实例---爬取下载喜马拉雅音频文件](https://blog.csdn.net/weixin_30701575/article/details/99085866)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值