您好,登錄后才能下訂單哦!
本篇內容主要講解“Python怎么爬取喜馬拉雅音頻數據”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Python怎么爬取喜馬拉雅音頻數據”吧!
爬取喜馬拉雅音頻數據
受害者地址
https://www.ximalaya.com/
本文知識點:
1、系統(tǒng)分析網頁性質
2、多層數據解析
3、海量音頻數據保存
環(huán)境:
python 3.6
pycharm
requests
parsel
1.確定數據所在的鏈接地址(url)
2.通過代碼發(fā)送url地址的請求
3.解析數據(要的, 篩選不要的)
4.數據持久化(保存)
案例思路:
1. 在靜態(tài)數據中獲取音頻的id值
2. 發(fā)送指定id值json數據請求(src)
3. 從json數據中解析音頻所對應的URL地址
先導入所需的模塊
import requests import parsel # 數據解析模塊 import re
1.確定數據所在的鏈接地址(url) 逆向分析 網頁性質(靜態(tài)網頁/動態(tài)網頁)
打開開發(fā)者工具,播放一個音頻,在Madie里面可以找到一個數據包
復制URL,搜索
繼續(xù)搜索,找到請求頭參數
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.解析數據(要的, 篩選不要的) 解析音頻的 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數據請求(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) 逆向分析 網頁性質(靜態(tài)網頁/動態(tài)網頁) 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.解析數據(要的, 篩選不要的) 解析音頻的 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數據請求(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怎么爬取喜馬拉雅音頻數據”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。