欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何用Python寫個(gè)聽(tīng)小說(shuō)的爬蟲(chóng)

 更新時(shí)間:2022年02月19日 16:17:14   作者:派森醬  
在路上經(jīng)常發(fā)現(xiàn)好多人都喜歡用耳機(jī)聽(tīng)小說(shuō),同事居然可以一整天的帶著一只耳機(jī)聽(tīng)小說(shuō)。本文就用Python爬蟲(chóng)實(shí)現(xiàn)下載聽(tīng)小說(shuō)tingchina.com的音頻,需要的可以參考一下

在路上發(fā)現(xiàn)好多人都喜歡用耳機(jī)聽(tīng)小說(shuō),同事居然可以一整天的帶著一只耳機(jī)聽(tīng)小說(shuō)。小編表示非常的震驚。今天就用 Python 下載聽(tīng)小說(shuō) tingchina.com的音頻。

書(shū)名和章節(jié)列表

隨機(jī)點(diǎn)開(kāi)一本書(shū),這個(gè)頁(yè)面可以使用 BeautifulSoup 獲取書(shū)名和所有單個(gè)章節(jié)音頻的列表。復(fù)制瀏覽器的地址,如:https://www.tingchina.com/yousheng/disp_31086.htm。

from?bs4?import?BeautifulSoup
import?requests
import?re
import?random
import?os

headers?=?{
????'user-agent':?'Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/91.0.4472.114?Safari/537.36'
}

def?get_detail_urls(url):
????url_list?=?[]
????response?=?requests.get(url,?headers=headers)
????response.encoding?=?'gbk'
????soup?=?BeautifulSoup(response.text,?'lxml')
????name?=?soup.select('.red12')[0].strong.text
????if?not?os.path.exists(name):
????????os.makedirs(name)
????div_list?=?soup.select('div.list?a')
????for?item?in?div_list:
????????url_list.append({'name':?item.string,?'url':?'https://www.tingchina.com/yousheng/{}'.format(item['href'])})
????return?name,?url_list

音頻地址

打開(kāi)單個(gè)章節(jié)的鏈接,在 Elements 面板用章節(jié)名稱作為搜索詞,在底部發(fā)現(xiàn)了一個(gè) script,這一部分就是聲源的地址。

在 Network 面板可以看到,聲源的 url 域名和章節(jié)列表的域名是不一樣的。在獲取下載鏈接的時(shí)候需要注意這一點(diǎn)。

def?get_mp3_path(url):
????response?=?requests.get(url,?headers=headers)
????response.encoding?=?'gbk'
????soup?=?BeautifulSoup(response.text,?'lxml')
????script_text?=?soup.select('script')[-1].string
????fileUrl_search?=?re.search('fileUrl=?"(.*?)";',?script_text,?re.S)
????if?fileUrl_search:
????????return?'https://t3344.tingchina.com'?+?fileUrl_search.group(1)

下載

驚喜總是突如其來(lái),把這個(gè) https://t3344.tingchina.com/xxxx.mp3 放入瀏覽器中運(yùn)行居然是 404。

肯定是少了關(guān)鍵性的參數(shù),回到上面 Network 仔細(xì)觀察 mp3 的 url,發(fā)現(xiàn)在 url 后面帶了一個(gè) key 的關(guān)鍵字。如下圖,這個(gè) key 是來(lái)自于 https://img.tingchina.com/play/h5_jsonp.asp?0.5078556568562795 的返回值,可以使用正則表達(dá)式將 key 取出來(lái)。

def?get_key(url):
????url?=?'https://img.tingchina.com/play/h5_jsonp.asp?{}'.format(str(random.random()))
????headers['referer']?=?url
????response?=?requests.get(url,?headers=headers)
????matched?=?re.search('(key=.*?)";',?response.text,?re.S)
????if?matched:
????????temp?=?matched.group(1)
????????return?temp[len(temp)-42:]

最后的最后在 __main__ 中將以上的代碼串聯(lián)起來(lái)。

if?__name__?==?"__main__":
????url?=?input("請(qǐng)輸入瀏覽器書(shū)頁(yè)的地址:")
????dir,url_list?=?get_detail_urls()

????for?item?in?url_list:
????????audio_url?=?get_mp3_path(item['url'])
????????key?=?get_key(item['url'])
????????audio_url?=?audio_url?+?'?key='?+?key
????????headers['referer']?=?item['url']
????????r?=?requests.get(audio_url,?headers=headers,stream=True)
????????with?open(os.path.join(dir,?item['name']),'ab')?as?f:
????????????f.write(r.content)
????????????f.flush()

完整代碼

from bs4 import BeautifulSoup
import requests
import re
import random
import os

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}

def get_detail_urls(url):
    url_list = []
    response = requests.get(url, headers=headers)
    response.encoding = 'gbk'
    soup = BeautifulSoup(response.text, 'lxml')
    name = soup.select('.red12')[0].strong.text
    if not os.path.exists(name):
        os.makedirs(name)
    div_list = soup.select('div.list a')
    for item in div_list:
        url_list.append({'name': item.string, 'url': 'https://www.tingchina.com/yousheng/{}'.format(item['href'])})
    return name, url_list
    
def get_mp3_path(url):
    response = requests.get(url, headers=headers)
    response.encoding = 'gbk'
    soup = BeautifulSoup(response.text, 'lxml')
    script_text = soup.select('script')[-1].string
    fileUrl_search = re.search('fileUrl= "(.*?)";', script_text, re.S)
    if fileUrl_search:
        return 'https://t3344.tingchina.com' + fileUrl_search.group(1)
        
def get_key(url):
    url = 'https://img.tingchina.com/play/h5_jsonp.asp?{}'.format(str(random.random()))
    headers['referer'] = url
    response = requests.get(url, headers=headers)
    matched = re.search('(key=.*?)";', response.text, re.S)
    if matched:
        temp = matched.group(1)
        return temp[len(temp)-42:]

if __name__ == "__main__":
    url = input("請(qǐng)輸入瀏覽器書(shū)頁(yè)的地址:")
    dir,url_list = get_detail_urls()

    for item in url_list:
        audio_url = get_mp3_path(item['url'])
        key = get_key(item['url'])
        audio_url = audio_url + '?key=' + key
        headers['referer'] = item['url']
        r = requests.get(audio_url, headers=headers,stream=True)
        with open(os.path.join(dir, item['name']),'ab') as f:
            f.write(r.content)
            f.flush()

總結(jié)

這個(gè) Python 爬蟲(chóng)比較簡(jiǎn)單,小編的每個(gè)月 30 元的流量都不夠用,有了這個(gè)小程序在地鐵上就可以不用流量聽(tīng)小說(shuō)了。

以上就是詳解如何用Python寫個(gè)聽(tīng)小說(shuō)的爬蟲(chóng)的詳細(xì)內(nèi)容,更多關(guān)于Python爬蟲(chóng) 聽(tīng)小說(shuō)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python的命令行參數(shù)實(shí)例詳解

    Python的命令行參數(shù)實(shí)例詳解

    python中有一個(gè)模塊sys,sys.argv這個(gè)屬性提供了對(duì)命令行參數(shù)的訪問(wèn),下面這篇文章主要給大家介紹了關(guān)于Python命令行參數(shù)實(shí)例的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • python字典的常用方法總結(jié)

    python字典的常用方法總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于python字典的常用方法以及相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。
    2019-07-07
  • 在Python中給Nan值更改為0的方法

    在Python中給Nan值更改為0的方法

    今天小編就為大家分享一篇在Python中給Nan值更改為0的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • python中將函數(shù)賦值給變量時(shí)需要注意的一些問(wèn)題

    python中將函數(shù)賦值給變量時(shí)需要注意的一些問(wèn)題

    變量賦值是我們?cè)谌粘i_(kāi)發(fā)中經(jīng)常會(huì)遇到的一個(gè)問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python中將函數(shù)賦值給變量時(shí)需要注意的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-08-08
  • 基于Python實(shí)現(xiàn)自制拼圖小游戲

    基于Python實(shí)現(xiàn)自制拼圖小游戲

    這篇文章主要為大家詳細(xì)介紹得了如何利用Python中pygame的這個(gè)非標(biāo)準(zhǔn)庫(kù)來(lái)做個(gè)小游戲-拼圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下
    2022-11-11
  • 使用PyCharm官方中文語(yǔ)言包漢化PyCharm

    使用PyCharm官方中文語(yǔ)言包漢化PyCharm

    這篇文章主要介紹了使用PyCharm官方中文語(yǔ)言包漢化PyCharm,需要的朋友可以參考下
    2020-11-11
  • Python RuntimeError: thread.__init__() not called解決方法

    Python RuntimeError: thread.__init__() not called解決方法

    這篇文章主要介紹了Python RuntimeError: thread.__init__() not called解決方法,需要的朋友可以參考下
    2015-04-04
  • 詳解python編程slice與indices函數(shù)用法示例

    詳解python編程slice與indices函數(shù)用法示例

    這篇文章主要介紹了詳解python編程中slice與indices使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-09-09
  • PyTorch中Tensor的拼接與拆分的實(shí)現(xiàn)

    PyTorch中Tensor的拼接與拆分的實(shí)現(xiàn)

    這篇文章主要介紹了PyTorch中Tensor的拼接與拆分的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python內(nèi)存泄漏排查技巧總結(jié)

    python內(nèi)存泄漏排查技巧總結(jié)

    這篇文章主要給大家分享了python內(nèi)存泄漏排查技巧總結(jié),工作過(guò)程中服務(wù)難免遇到內(nèi)存泄漏問(wèn)題,下面文章就給大家總結(jié)一些排查下技巧,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2021-12-12

最新評(píng)論