這幾天想看龍族最新版本,但是搜索半天發現 沒有網站提供 下載, 我又只想下載後離線閱讀(寫代碼已經很費眼睛了)。無奈只有自己 爬取了。
這裏記錄一下,以後想看時,直接運行腳本 下載小說。
這裏是從 http://longzu5.co 這個網站下載的小說,如果需要更改存儲路徑,可以更改 FILE_URL 常量的值
如果 爬取不到了,說明,此網站做了防爬蟲,或者 其渲染 網頁的 html 元素改變了。
# -*- coding: utf-8 -*- # (C) rgc, 2018 # All rights reserved # requirements list: [python3.6, requests, bs4] import requests from bs4 import BeautifulSoup URL = "http://longzu5.co" FILE_URL = 'E:\lz.txt' def get_son_text(strs): # 獲取文章內容 soup = BeautifulSoup(strs, 'html.parser') body_soup = soup.find('div', 'post-body') result = body_soup.find_all('p') title = soup.find('h2', 'post-title') title = title.text final_txt = title + '\n' for item in result: txt = item.text final_txt += txt final_txt += '\n\n' with open(FILE_URL, 'a', encoding='utf-8') as f: f.write(final_txt) def get_father_text(): """ 獲取文章列表 :return: """ res = requests.get(URL + "/") strs = res.text soup = BeautifulSoup(strs, 'html.parser') ul_soup = soup.find('ul', 'booklist') x = ul_soup.find_all('a') section_list = [] for item in x: url = URL + item.get('href') section_list.append(url) section_list.reverse() for url in section_list: print(url) section = requests.get(url) sec_txt = section.text get_son_text(sec_txt) if __name__ == '__main__': get_father_text()
# 如有版權,請及時聯繫我,我會及時刪除,如有冒犯,請原諒。