乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解

       老三的休閑書(shū)屋 2020-12-07

      前言

      今天帶大家爬取喜馬拉雅音頻數(shù)據(jù),一起期待吧!!

      這個(gè)案例的視頻地址在這里

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

      項(xiàng)目目標(biāo)

      爬取喜馬拉雅音頻數(shù)據(jù)

      受害者地址

      https://www.ximalaya.com/
      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解

      本文知識(shí)點(diǎn):

      • 1、系統(tǒng)分析網(wǎng)頁(yè)性質(zhì)
      • 2、多層數(shù)據(jù)解析
      • 3、海量音頻數(shù)據(jù)保存

      環(huán)境:

      • python 3.6
      • pycharm
      • requests
      • parsel

      思路:(爬蟲(chóng)案例)

      • 1.確定數(shù)據(jù)所在的鏈接地址(url)
      • 2.通過(guò)代碼發(fā)送url地址的請(qǐng)求
      • 3.解析數(shù)據(jù)(要的, 篩選不要的)
      • 4.數(shù)據(jù)持久化(保存)

      案例思路:

      • 1. 在靜態(tài)數(shù)據(jù)中獲取音頻的id值
      • 2. 發(fā)送指定id值json數(shù)據(jù)請(qǐng)求(src)
      • 3. 從json數(shù)據(jù)中解析音頻所對(duì)應(yīng)的URL地址

      開(kāi)始寫(xiě)代碼

      先導(dǎo)入所需的模塊

      import requestsimport parsel # 數(shù)據(jù)解析模塊import re

      1.確定數(shù)據(jù)所在的鏈接地址(url) 逆向分析 網(wǎng)頁(yè)性質(zhì)(靜態(tài)網(wǎng)頁(yè)/動(dòng)態(tài)網(wǎng)頁(yè))

      打開(kāi)開(kāi)發(fā)者工具,播放一個(gè)音頻,在Madie里面可以找到一個(gè)數(shù)據(jù)包

      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解

      復(fù)制URL,搜索

      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解

      找到ID值

      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解

      繼續(xù)搜索,找到請(qǐng)求頭參數(shù)

      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解
      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.通過(guò)代碼發(fā)送url地址的請(qǐng)求

      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ù)請(qǐng)求(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)        # 請(qǐng)求音頻數(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 reimport requestsimport parsel # 數(shù)據(jù)解析模塊def change_title(title): '''處理文件名非法字符的方法''' pattern = re.compile(r'[\/\\\:\*\?\'\<\>\|]') # '/ \ : * ? ' < > |' new_title = re.sub(pattern, '_', title) # 替換為下劃線 return new_titlefor page in range(13, 33): print('---------------正在爬取第{}頁(yè)的數(shù)據(jù)----------------'.format(page)) # 1.確定數(shù)據(jù)所在的鏈接地址(url) 逆向分析 網(wǎng)頁(yè)性質(zhì)(靜態(tài)網(wǎng)頁(yè)/動(dòng)態(tài)網(wǎng)頁(yè)) 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.通過(guò)代碼發(fā)送url地址的請(qǐng)求 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ù)請(qǐng)求(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) # 請(qǐng)求音頻數(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

      運(yùn)行代碼,效果如下圖

      Python爬蟲(chóng)實(shí)戰(zhàn)案例:取喜馬拉雅音頻數(shù)據(jù)詳解

      完整代碼后臺(tái)私信小編01即可

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多