Python爬虫实战案例之爬取喜马拉雅音频数据详解

(编辑:jimmy 日期: 2024/9/21 浏览:2)

前言

喜马拉雅是专业的音频分享平台,汇集了有声小说,有声读物,有声书,FM电台,儿童睡前故事,相声小品,鬼故事等数亿条音频,我最喜欢听民间故事和德云社相声集,你呢?

今天带大家爬取喜马拉雅音频数据,一起期待吧!!

这个案例的视频地址在这里

https://v.douyu.com/show/a2JEMJj3e3mMNxml

项目目标

爬取喜马拉雅音频数据

受害者地址

https://www.ximalaya.com/

Python爬虫实战案例之爬取喜马拉雅音频数据详解

本文知识点:

1、系统分析网页性质

2、多层数据解析

3、海量音频数据保存

环境:

1.确定数据所在的链接地址(url)
2.通过代码发送url地址的请求
3.解析数据(要的, 筛选不要的)
4.数据持久化(保存)

案例思路:

1. 在静态数据中获取音频的id值

2. 发送指定id值json数据请求(src)

3. 从json数据中解析音频所对应的URL地址 开始写代码

先导入所需的模块

import requests
import parsel # 数据解析模块
import re

1.确定数据所在的链接地址(url) 逆向分析 网页性质(静态网页/动态网页)

打开开发者工具,播放一个音频,在Madie里面可以找到一个数据包

Python爬虫实战案例之爬取喜马拉雅音频数据详解

复制URL,搜索

Python爬虫实战案例之爬取喜马拉雅音频数据详解

找到ID值

Python爬虫实战案例之爬取喜马拉雅音频数据详解

继续搜索,找到请求头参数

Python爬虫实战案例之爬取喜马拉雅音频数据详解

url = 'https://www.ximalaya.com/youshengshu/4256765/p{}/'.format(page)
headers = {
 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}

2.通过代码发送url地址的请求

response = requests.get(url=url, headers=headers)
html_data = response.text

3.解析数据(要的, 筛选不要的) 解析音频的 id值

selector = parsel.Selector(html_data)
lis = selector.xpath('//div[@class="sound-list _is"]/ul/li')

for li in lis:
 try:
  title = li.xpath('.//a/@title').get() + '.m4a'
  href = li.xpath('.//a/@href').get()
  # print(title, href)

  m4a_id = href.split('/')[-1]
  # print(href, m4a_id)

  # 发送指定id值json数据请求(src)
  json_url = 'https://www.ximalaya.com/revision/play/v1/audio"htmlcode">
with open('video\\' + new_title, mode='wb') as f:
 f.write(m4a_data)
 print('保存完成:', title)

最后还要处理文件名非法字符

def change_title(title):
 pattern = re.compile(r"[\/\\\:\*\"\<\>\|]") # '/ \ : * " < > |'
 new_title = re.sub(pattern, "_", title) # 替换为下划线
 return new_title

完整代码

import re

import requests
import parsel # 数据解析模块


def change_title(title):
 """处理文件名非法字符的方法"""
 pattern = re.compile(r"[\/\\\:\*\"\<\>\|]") # '/ \ : * " < > |'
 new_title = re.sub(pattern, "_", title) # 替换为下划线
 return new_title


for page in range(13, 33):
 print('---------------正在爬取第{}页的数据----------------'.format(page))
 # 1.确定数据所在的链接地址(url) 逆向分析 网页性质(静态网页/动态网页)
 url = 'https://www.ximalaya.com/youshengshu/4256765/p{}/'.format(page)
 headers = {
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}

 # 2.通过代码发送url地址的请求
 response = requests.get(url=url, headers=headers)
 html_data = response.text
 # print(html_data)

 # 3.解析数据(要的, 筛选不要的) 解析音频的 id值
 selector = parsel.Selector(html_data)
 lis = selector.xpath('//div[@class="sound-list _is"]/ul/li')

 for li in lis:
  try:
   title = li.xpath('.//a/@title').get() + '.m4a'
   href = li.xpath('.//a/@href').get()
   # print(title, href)

   m4a_id = href.split('/')[-1]
   # print(href, m4a_id)

   # 发送指定id值json数据请求(src)
   json_url = 'https://www.ximalaya.com/revision/play/v1/audio"text-align: center">Python爬虫实战案例之爬取喜马拉雅音频数据详解

一句话新闻

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。