自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用 50 行代碼寫個(gè)聽小說(shuō)的爬蟲

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

[[407938]]

本文轉(zhuǎn)載自微信公眾號(hào)「Python技術(shù)」,作者派森醬。轉(zhuǎn)載本文請(qǐng)聯(lián)系Python技術(shù)公眾號(hào)。

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

書名和章節(jié)列表

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

  1. from bs4 import BeautifulSoup 
  2. import requests 
  3. import re 
  4. import random 
  5. import os 
  6.  
  7. headers = { 
  8.     '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' 
  9.  
  10. def get_detail_urls(url): 
  11.     url_list = [] 
  12.     response = requests.get(url, headers=headers) 
  13.     response.encoding = 'gbk' 
  14.     soup = BeautifulSoup(response.text, 'lxml'
  15.     name = soup.select('.red12')[0].strong.text 
  16.     if not os.path.exists(name): 
  17.         os.makedirs(name
  18.     div_list = soup.select('div.list a'
  19.     for item in div_list: 
  20.         url_list.append({'name': item.string, 'url''https://www.tingchina.com/yousheng/{}'.format(item['href'])}) 
  21.     return name, url_list 

音頻地址

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

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

  1. def get_mp3_path(url): 
  2.     response = requests.get(url, headers=headers) 
  3.     response.encoding = 'gbk' 
  4.     soup = BeautifulSoup(response.text, 'lxml'
  5.     script_text = soup.select('script')[-1].string 
  6.     fileUrl_search = re.search('fileUrl= "(.*?)";', script_text, re.S) 
  7.     if fileUrl_search: 
  8.         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)。

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

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

  1. if __name__ == "__main__"
  2.     url = input("請(qǐng)輸入瀏覽器書頁(yè)的地址:"
  3.     dir,url_list = get_detail_urls() 
  4.  
  5.     for item in url_list: 
  6.         audio_url = get_mp3_path(item['url']) 
  7.         key = get_key(item['url']) 
  8.         audio_url = audio_url + '?key=' + key 
  9.         headers['referer'] = item['url'
  10.         r = requests.get(audio_url, headers=headers,stream=True
  11.         with open(os.path.join(dir, item['name']),'ab'as f: 
  12.             f.write(r.content) 
  13.             f.flush() 

總結(jié)

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

 

責(zé)任編輯:武曉燕 來(lái)源: Python技術(shù)
相關(guān)推薦

2019-10-17 21:37:28

微信飛機(jī)大戰(zhàn)Python

2018-01-23 09:17:22

Python人臉識(shí)別

2023-02-01 22:40:38

shellDocker

2021-10-28 09:42:38

代碼編碼開發(fā)

2018-06-19 08:35:51

情感分析數(shù)據(jù)集代碼

2011-05-03 09:10:12

項(xiàng)目管理程序員

2018-01-30 22:07:18

Python區(qū)塊鏈比特幣

2017-11-24 08:30:05

Python代碼區(qū)塊鏈

2021-04-01 09:02:38

Python小說(shuō)下載網(wǎng)絡(luò)爬蟲

2021-09-13 16:40:30

Java C 語(yǔ)言游戲

2020-03-26 12:38:15

代碼節(jié)點(diǎn)數(shù)據(jù)

2013-03-04 10:22:30

Python

2018-06-29 10:15:20

PythonOpenCV人臉識(shí)別

2022-11-04 11:44:56

WebFluxCURDWeb

2023-10-31 08:21:18

WebFlux基本用法JPA

2022-03-23 10:21:56

Python代碼工具

2014-06-19 10:02:32

Haskell代碼

2014-01-09 09:42:56

Python語(yǔ)言檢測(cè)器

2020-07-20 09:20:48

代碼geventPython

2020-06-01 13:49:16

Python代碼3D地圖
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)