Python爬蟲實戰(zhàn)案例之爬取喜馬拉雅音頻數(shù)據(jù)詳解
前言
喜馬拉雅是專業(yè)的音頻分享平臺,匯集了有聲小說,有聲讀物,有聲書,FM電臺,兒童睡前故事,相聲小品,鬼故事等數(shù)億條音頻,我最喜歡聽民間故事和德云社相聲集,你呢?
今天帶大家爬取喜馬拉雅音頻數(shù)據(jù),一起期待吧??!
這個案例的視頻地址在這里
https://v.douyu.com/show/a2JEMJj3e3mMNxml
項目目標
爬取喜馬拉雅音頻數(shù)據(jù)
受害者地址
本文知識點:
1、系統(tǒng)分析網(wǎng)頁性質(zhì)
2、多層數(shù)據(jù)解析
3、海量音頻數(shù)據(jù)保存
環(huán)境:
1.確定數(shù)據(jù)所在的鏈接地址(url)
2.通過代碼發(fā)送url地址的請求
3.解析數(shù)據(jù)(要的, 篩選不要的)
4.數(shù)據(jù)持久化(保存)
案例思路:
1. 在靜態(tài)數(shù)據(jù)中獲取音頻的id值
2. 發(fā)送指定id值json數(shù)據(jù)請求(src)
3. 從json數(shù)據(jù)中解析音頻所對應(yīng)的URL地址 開始寫代碼
先導(dǎo)入所需的模塊
import requests import parsel # 數(shù)據(jù)解析模塊 import re
1.確定數(shù)據(jù)所在的鏈接地址(url) 逆向分析 網(wǎng)頁性質(zhì)(靜態(tài)網(wǎng)頁/動態(tài)網(wǎng)頁)
打開開發(fā)者工具,播放一個音頻,在Madie里面可以找到一個數(shù)據(jù)包
復(fù)制URL,搜索
找到ID值
繼續(xù)搜索,找到請求頭參數(shù)
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.通過代碼發(fā)送url地址的請求
response = requests.get(url=url, headers=headers) html_data = response.text
3.解析數(shù)據(jù)(要的, 篩選不要的) 解析音頻的 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) # 發(fā)送指定id值json數(shù)據(jù)請求(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) # 請求音頻數(shù)據(jù) m4a_data = requests.get(url=m4a_url, headers=headers).content new_title = change_title(title)
4.數(shù)據(jù)持久化(保存)
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 # 數(shù)據(jù)解析模塊 def change_title(title): """處理文件名非法字符的方法""" pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]") # '/ \ : * ? " < > |' new_title = re.sub(pattern, "_", title) # 替換為下劃線 return new_title for page in range(13, 33): print('---------------正在爬取第{}頁的數(shù)據(jù)----------------'.format(page)) # 1.確定數(shù)據(jù)所在的鏈接地址(url) 逆向分析 網(wǎng)頁性質(zhì)(靜態(tài)網(wǎng)頁/動態(tài)網(wǎng)頁) 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.通過代碼發(fā)送url地址的請求 response = requests.get(url=url, headers=headers) html_data = response.text # print(html_data) # 3.解析數(shù)據(jù)(要的, 篩選不要的) 解析音頻的 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) # 發(fā)送指定id值json數(shù)據(jù)請求(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) # 請求音頻數(shù)據(jù) m4a_data = requests.get(url=m4a_url, headers=headers).content new_title = change_title(title) # print(new_title) # 4.數(shù)據(jù)持久化(保存) with open('video\\' + new_title, mode='wb') as f: f.write(m4a_data) print('保存完成:', title) except: pass
運行代碼,效果如下圖
到此這篇關(guān)于Python爬蟲實戰(zhàn)案例之取喜馬拉雅音頻數(shù)據(jù)詳解的文章就介紹到這了,更多相關(guān)Python爬取喜馬拉雅音頻數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python7個爬蟲小案例詳解(附源碼)下篇
- Python7個爬蟲小案例詳解(附源碼)中篇
- Python7個爬蟲小案例詳解(附源碼)上篇
- 利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析
- Python爬蟲采集Tripadvisor數(shù)據(jù)案例實現(xiàn)
- Python?Ajax爬蟲案例分享
- Python爬蟲入門案例之爬取去哪兒旅游景點攻略以及可視化分析
- Python爬蟲入門案例之爬取二手房源數(shù)據(jù)
- Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
- Python爬蟲之Scrapy環(huán)境搭建案例教程
- 用Python爬蟲破解滑動驗證碼的案例解析
- python爬蟲系列網(wǎng)絡(luò)請求案例詳解
- python爬蟲破解字體加密案例詳解
- python爬蟲線程池案例詳解(梨視頻短視頻爬取)
- python爬蟲scrapy框架的梨視頻案例解析
- python爬蟲利器之requests庫的用法(超全面的爬取網(wǎng)頁案例)
- Python爬蟲Scrapy框架CrawlSpider原理及使用案例
- Python爬蟲之對CSDN榜單進行分析
相關(guān)文章
使用PowerShell實現(xiàn)批量修改或替換文件名
這篇文章主要為大家介紹了基于PowerShell語言,對文件夾中全部文件的名稱加以批量替換、修改的方法,文中的示例代碼講解詳細,感興趣的可以了解一下2023-04-04Django接受前端數(shù)據(jù)的幾種方法總結(jié)
下面小編就為大家?guī)硪黄狣jango接受前端數(shù)據(jù)的幾種方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11python requests更換代理適用于IP頻率限制的方法
今天小編就為大家分享一篇python requests更換代理適用于IP頻率限制的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08python中數(shù)據(jù)爬蟲requests庫使用方法詳解
本篇文章主要介紹了python中數(shù)據(jù)爬蟲requests庫使用方法詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02