1. 程式人生 > 程式設計 >Python爬蟲實戰案例之爬取喜馬拉雅音訊資料詳解

Python爬蟲實戰案例之爬取喜馬拉雅音訊資料詳解

前言

喜馬拉雅是專業的音訊分享平臺,彙集了有聲小說,有聲讀物,有聲書,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?id={}&ptype=1'.format(m4a_id)
  json_data = requests.get(url=json_url,headers=headers).json()
  # print(json_data)

  # 提取音訊地址
  m4a_url = json_data['data']['src']
  # print(m4a_url)

  # 請求音訊資料
  m4a_data = requests.get(url=m4a_url,headers=headers).content

  new_title = change_title(title)

4.資料持久化(儲存)

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?id={}&ptype=1'.format(m4a_id)
   json_data = requests.get(url=json_url,headers=headers).json()
   # print(json_data)

   # 提取音訊地址
   m4a_url = json_data['data']['src']
   # print(m4a_url)

   # 請求音訊資料
   m4a_data = requests.get(url=m4a_url,headers=headers).content

   new_title = change_title(title)
   # print(new_title)

   # 4.資料持久化(儲存)
   with open('video\\' + new_title,mode='wb') as f:
    f.write(m4a_data)
    print('儲存完成:',title)
  except:
   pass

執行程式碼,效果如下圖

Python爬蟲實戰案例之爬取喜馬拉雅音訊資料詳解

到此這篇關於Python爬蟲實戰案例之取喜馬拉雅音訊資料詳解的文章就介紹到這了,更多相關Python爬取喜馬拉雅音訊資料內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!