前言💨
本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理。
前文內容💨
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)