這篇文章主要介紹了怎么用Python寫個聽小說的爬蟲的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么用Python寫個聽小說的爬蟲文章都會有所收獲,下面我們一起來看看吧。
隨機點開一本書,這個頁面可以使用 BeautifulSoup 獲取書名和所有單個章節音頻的列表。復制瀏覽器的地址,如: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
打開單個章節的鏈接,在 Elements 面板用章節名稱作為搜索詞,在底部發現了一個 script,這一部分就是聲源的地址。
在 Network 面板可以看到,聲源的 url 域名和章節列表的域名是不一樣的。在獲取下載鏈接的時候需要注意這一點。
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)
驚喜總是突如其來,把這個 https://t3344.tingchina.com/xxxx.mp3 放入瀏覽器中運行居然是 404。
肯定是少了關鍵性的參數,回到上面 Network 仔細觀察 mp3 的 url,發現在 url 后面帶了一個 key 的關鍵字。如下圖,這個 key 是來自于 https://img.tingchina.com/play/h6_jsonp.asp?0.5078556568562795 的返回值,可以使用正則表達式將 key 取出來。
def get_key(url): url = 'https://img.tingchina.com/play/h6_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__
中將以上的代碼串聯起來。
if __name__ == "__main__": url = input("請輸入瀏覽器書頁的地址:") 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/h6_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("請輸入瀏覽器書頁的地址:") 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()
關于“怎么用Python寫個聽小說的爬蟲”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“怎么用Python寫個聽小說的爬蟲”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。