python下載小說

????最近正好在看這本小說,網(wǎng)上廣告屬實多了點,而且好多存在斷章的情況,所以自己去網(wǎng)上下載下來電腦或者手機(jī)上看最實際了

1. 首頁

????對沒錯,就是這本都市小說《道德天書》,從首頁還是很容易能夠獲取到章節(jié)鏈接的

image.png

2. 內(nèi)容頁

????內(nèi)容頁面有點小陷阱,雖然看著簡單,但是實際將頁面的內(nèi)容打印出來是殘缺的,他只構(gòu)建了部分文本內(nèi)容,實際的內(nèi)容是需要自己抓包獲取,不信的話可以打印頁面內(nèi)容看看,是不完整的

image.png

3. 抓包

這里規(guī)則還是比較簡單的,很容易就找到了對應(yīng)的數(shù)據(jù)包

image.png

我們選擇數(shù)據(jù)包的headers進(jìn)入里面顯示的真實鏈接就可以看到具體的內(nèi)容了

image.png

4. 數(shù)據(jù)獲取

???? 所有的數(shù)據(jù)來源我們都知道了,就著手開始建設(shè)了,首先從首頁遍歷所有章節(jié)的頁面鏈接,從每個章節(jié)的頁面中獲取到標(biāo)題和內(nèi)容,沒錯,這里的內(nèi)容需要去數(shù)據(jù)包中獲取,仔細(xì)觀察會發(fā)現(xiàn)數(shù)據(jù)包的鏈接恰巧就是網(wǎng)頁主頁鏈接+章節(jié)鏈接的后兩項,所以能夠很輕易的組合出來,后面的內(nèi)容無非是獲取,寫入。

  • 這里因為內(nèi)容中還是存在廣告,所以用replace將它剔除了,其他的就是xpath解析,寫入了
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@author: maya
@software: Pycharm
@file: tqdm.py
@time: 2019/8/20 14:08
@desc:
'''
import requests
from lxml import etree

headers = {
    'cookie': 'Hm_lvt_33b927fed41089db72f5d741701b24f2=1566285504; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; Hm_lpvt_33b927fed41089db72f5d741701b24f2=1566285551',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
    'upgrade-insecure-request': '1',
    'referer': 'https://www.rzlib.net/b/73/73530/',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
}

def get_html(url):
    return requests.get(url, headers=headers)\
        .text.replace('如果您覺得《周睿道德天書》還不錯的話,請粘貼以下網(wǎng)址分享給你的QQ、'
                      '微信或微博好友,謝謝支持!', '').replace('( 本書網(wǎng)址:https://www.'
                                                   'rzlib.net/b/73/73530/ )', '')


def get_data(url):
    html = etree.HTML(get_html(url))
    title = html.xpath('//h1/text()')[0].replace('.', '_')
    content_url = get_url(url)
    content_html = etree.HTML(get_html(content_url)).xpath('//body//text()')
    content = ["".join(data.split()) for data in content_html]
    return title, content


def get_url(url):
    return "https://www.rzlib.net/b/txtt5552/" + url.split('/')[-2] + "/" + url.split('/')[-1]


def write_data(url):
    title, content = get_data(url)
    with open('books/' + title + '.txt', 'w', encoding='utf-8') as f:
        f.write(title.replace('_', '. ') + '\n')
        for data in content:
            if data != "":
                f.write('  ' + data + '\n')
    with open('books/books.txt', 'a', encoding='utf-8') as p:
        p.write(title.replace('_', '. ') + '\n')
        for data in content:
            if data != "":
                p.write('  ' + data + '\n')
        p.write('\n')



def get_total(index_utl):
    html = etree.HTML(get_html(index_utl))
    urls = html.xpath('//div[@class="ListChapter"][2]/ul/li/a/@href')
    for url in urls:
        write_data("https://www.rzlib.net" + url)
        print("第{}章已完成寫入".format(urls.index(url) + 1))


if __name__ == '__main__':
    get_total("https://www.rzlib.net/b/73/73530/")

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 使用了requests,下載的筆趣閣的小說 # -*- coding: utf-8 -*- import requ...
    Blue_Color閱讀 599評論 0 0
  • 下載新筆趣閣小說 采用了requests庫,代碼修改一下文件存放的文章,以及添加一下小說目錄就可以使用.代碼百度云
    知識學(xué)者閱讀 1,296評論 4 0
  • 簡介 用簡單的話來定義tcpdump,就是:dump the traffic on a network,根據(jù)使用者...
    JasonShi6306421閱讀 1,325評論 0 1
  • 簡介 用簡單的話來定義tcpdump,就是:dump the traffic on a network,根據(jù)使用者...
    保川閱讀 6,060評論 1 13
  • 我不知你去了哪里 文/古爾邦 我從未見過的爺爺 聽說無常于我的父親還在襁褓中的歲月 我不知你去了哪里 我小學(xué)的老師...
    古爾邦閱讀 649評論 3 29

友情鏈接更多精彩內(nèi)容