最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)網(wǎng)頁內(nèi)容轉(zhuǎn)純文本與EPUB電子書的完整指南

 更新時間:2026年02月03日 11:24:58   作者:站大爺IP  
在信息爆炸的時代,我們每天都會瀏覽大量網(wǎng)頁內(nèi)容,本文將通過Python實現(xiàn)兩種主流保存方案,即純文本格式TXT和電子書標準格式EPUB,感興趣的小伙伴可以了解一下

在信息爆炸的時代,我們每天都會瀏覽大量網(wǎng)頁內(nèi)容。無論是新聞資訊、技術(shù)教程還是小說文學,如何將這些分散的網(wǎng)頁內(nèi)容高效保存并離線閱讀,成為現(xiàn)代人的剛需。本文將通過Python實現(xiàn)兩種主流保存方案:純文本格式(TXT)和電子書標準格式(EPUB),并提供完整代碼與實戰(zhàn)案例。

一、技術(shù)選型與工具準備

1.1 核心工具鏈

  • 網(wǎng)頁抓取requests庫(HTTP請求)
  • HTML解析BeautifulSoup(靜態(tài)內(nèi)容解析)
  • 電子書生成ebooklib(EPUB標準支持)
  • 動態(tài)內(nèi)容處理Selenium(可選,應(yīng)對JavaScript渲染)

1.2 環(huán)境配置

pip install requests beautifulsoup4 lxml ebooklib selenium

注:若需處理動 態(tài)網(wǎng)頁,還需下載對應(yīng)瀏覽器的WebDriver(如ChromeDriver)

二、網(wǎng)頁內(nèi)容提取技術(shù)

2.1 基礎(chǔ)抓取流程

import requests
from bs4 import BeautifulSoup

def fetch_webpage(url):
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(url, headers=headers)
    response.raise_for_status()  # 異常處理
    return response.content

2.2 智能正文提取算法

通過分析100+新聞/博客網(wǎng)站結(jié)構(gòu),總結(jié)出以下高效提取策略:

def extract_content(html):
    soup = BeautifulSoup(html, 'lxml')
    
    # 優(yōu)先匹配常見正文容器
    selectors = ['article', 'main', '.post-content', '#content']
    for selector in selectors:
        content = soup.select_one(selector)
        if content:
            return clean_content(content)
    
    # 回退方案:提取所有段落
    paragraphs = soup.find_all('p')
    if paragraphs:
        return '\n\n'.join(p.get_text() for p in paragraphs)
    
    return "提取失敗"

def clean_content(element):
    # 移除廣告/分享按鈕等干擾元素
    for tag in ['script', 'style', 'iframe', 'nav', 'footer']:
        for item in element.find_all(tag):
            item.decompose()
    return '\n\n'.join(p.get_text() for p in element.find_all('p'))

實戰(zhàn)案例:提取CSDN博客正文

url = "https://blog.csdn.net/example/article/details/123456"
html = fetch_webpage(url)
content = extract_content(html)
print(content[:200])  # 預(yù)覽前200字符

三、純文本保存方案

3.1 基礎(chǔ)實現(xiàn)

def save_as_txt(title, content, filename=None):
    if not filename:
        safe_title = "".join(c for c in title if c.isalnum() or c in (' ', '_'))[:50]
        filename = f"{safe_title}.txt"
    
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(f"【標題】{title}\n\n")
        f.write(content)
    print(f"? 保存成功: {filename}")

3.2 增強功能

  • 自動分章節(jié):通過標題標簽(h1-h6)分割內(nèi)容
  • 編碼優(yōu)化:處理特殊字符與換行符
  • 批量處理:爬取多篇文章合并保存

完整示例

def advanced_txt_save(url):
    html = fetch_webpage(url)
    soup = BeautifulSoup(html, 'lxml')
    
    # 提取標題與正文
    title = soup.title.string if soup.title else "無標題"
    content = extract_content(html)
    
    # 自動分章節(jié)(示例:按h2分割)
    chapters = []
    current_chapter = ""
    for element in soup.body.descendants:
        if element.name == 'h2':
            if current_chapter:
                chapters.append(current_chapter)
            current_chapter = f"\n\n{element.get_text()}\n"
        elif element.name == 'p' and current_chapter is not None:
            current_chapter += f"{element.get_text()}\n"
    if current_chapter:
        chapters.append(current_chapter)
    
    # 保存
    with open(f"{title}.txt", 'w', encoding='utf-8') as f:
        f.write(f"【全文目錄】\n")
        for i, chap in enumerate(chapters, 1):
            f.write(f"{i}. {chap.split('\n')[0]}\n")
        f.write("\n" + "\n".join(chapters))

四、EPUB電子書生成技術(shù)

4.1 EPUB標準解析

EPUB是國際數(shù)字出版論壇(IDPF)制定的開放標準,具有三大核心組件:

  • 內(nèi)容文檔:XHTML格式的章節(jié)文件
  • 包裝文件:OPF(Open Packaging Format)
  • 導航文件:NCX(Navigation Control file)

4.2 基礎(chǔ)生成代碼

from ebooklib import epub

def create_epub(title, author, content):
    book = epub.EpubBook()
    book.set_identifier('id123456')
    book.set_title(title)
    book.set_language('zh-CN')
    book.add_author(author)
    
    # 創(chuàng)建章節(jié)
    chapter = epub.EpubHtml(
        title=title,
        file_name='content.xhtml',
        lang='zh'
    )
    html_content = f"""
    <html>
        <head>
            <title>{title}</title>
            <style>
                body {{ font-family: "SimSun"; line-height: 1.6; }}
                h1 {{ text-align: center; }}
                p {{ text-indent: 2em; margin: 0.5em 0; }}
            </style>
        </head>
        <body>
            <h1>{title}</h1>
            {"".join(f"<p>{para}</p>" for para in content.split('\n\n') if para.strip())}
        </body>
    </html>
    """
    chapter.set_content(html_content)
    book.add_item(chapter)
    
    # 設(shè)置目錄
    book.toc = [epub.Link('content.xhtml', title, 'intro')]
    book.spine = ['nav', chapter]
    
    # 添加導航文件
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    
    return book

def save_epub(book, filename=None):
    if not filename:
        filename = f"{book.title[:50]}.epub"
    epub.write_epub(filename, book)
    print(f"? EPUB生成成功: {filename}")

4.3 高級功能實現(xiàn)

案例:多章節(jié)小說生成

def novel_to_epub(url_template, chapters):
    book = epub.EpubBook()
    book.set_identifier('novel123')
    book.set_title("示例小說")
    book.set_language('zh')
    book.add_author("佚名")
    
    # 批量添加章節(jié)
    for i, chap_num in enumerate(chapters, 1):
        url = url_template.format(chap_num)
        html = fetch_webpage(url)
        soup = BeautifulSoup(html, 'lxml')
        
        # 假設(shè)每章標題在h1標簽
        title = soup.h1.get_text() if soup.h1 else f"第{chap_num}章"
        content = clean_content(soup)
        
        chapter = epub.EpubHtml(
            title=title,
            file_name=f"chap_{i}.xhtml"
        )
        chapter.set_content(f"""
        <html>
            <body>
                <h2>{title}</h2>
                {"".join(f"<p>{para}</p>" for para in content.split('\n\n') if para.strip())}
            </body>
        </html>
        """)
        book.add_item(chapter)
        book.toc.append(epub.Link(f"chap_{i}.xhtml", title, f"chap{i}"))
    
    book.spine = ['nav'] + [epub.SpineItem(ref=f"chap_{i}.xhtml") for i in range(1, len(chapters)+1)]
    save_epub(book)

# 使用示例
novel_to_epub("https://example.com/novel/chapter_{}.html", range(1, 21))

五、實戰(zhàn)綜合案例

5.1 需求場景

將知乎專欄的多篇文章合并保存為EPUB,要求:

  • 自動抓取指定專欄的所有文章
  • 保留原文格式與圖片
  • 生成可跳轉(zhuǎn)的目錄

5.2 解決方案

import os
from urllib.parse import urljoin

def zhihu_column_to_epub(column_url):
    # 1. 獲取專欄文章列表(簡化版,實際需分析知乎API)
    html = fetch_webpage(column_url)
    soup = BeautifulSoup(html, 'lxml')
    article_links = [urljoin(column_url, a['href']) 
                    for a in soup.select('.List-itemTitle a[href^="/p/"]')[:5]]  # 取前5篇示例
    
    # 2. 創(chuàng)建EPUB容器
    book = epub.EpubBook()
    book.set_identifier('zhihu123')
    column_title = soup.select_one('.ColumnHeader-title').get_text().strip()
    book.set_title(column_title)
    book.set_language('zh')
    book.add_author("知乎用戶")
    
    # 3. 處理每篇文章
    for i, url in enumerate(article_links, 1):
        article_html = fetch_webpage(url)
        article_soup = BeautifulSoup(article_html, 'lxml')
        
        # 提取標題與正文
        title = article_soup.select_one('h1.Post-title').get_text().strip()
        content_div = article_soup.select_one('.Post-RichText')
        
        # 處理圖片(保存到本地并修改路徑)
        img_dir = f"images_{i}"
        os.makedirs(img_dir, exist_ok=True)
        for img in content_div.find_all('img'):
            img_url = urljoin(url, img['src'])
            img_data = requests.get(img_url).content
            img_name = img['src'].split('/')[-1]
            with open(f"{img_dir}/{img_name}", 'wb') as f:
                f.write(img_data)
            img['src'] = f"{img_dir}/{img_name}"
        
        # 生成章節(jié)
        chapter = epub.EpubHtml(
            title=title,
            file_name=f"article_{i}.xhtml"
        )
        chapter.set_content(f"""
        <html>
            <head>
                <style>
                    body {{ max-width: 800px; margin: 0 auto; font-family: "SimSun"; }}
                    img {{ max-width: 100%; height: auto; }}
                </style>
            </head>
            <body>
                <h1>{title}</h1>
                {str(content_div)}
            </body>
        </html>
        """)
        book.add_item(chapter)
        book.toc.append(epub.Link(f"article_{i}.xhtml", title, f"art{i}"))
    
    # 4. 生成EPUB
    book.spine = ['nav'] + [epub.SpineItem(ref=f"article_{i}.xhtml") for i in range(1, len(article_links)+1)]
    save_epub(book, f"{column_title}.epub")

# 使用示例
zhihu_column_to_epub("https://zhuanlan.zhihu.com/example")

六、性能優(yōu)化與異常處理

6.1 常見問題解決方案

問題類型解決方案
反爬機制設(shè)置User-Agent,使用代理IP池
動態(tài)內(nèi)容結(jié)合Selenium渲染JavaScript
編碼錯誤強制指定response.encoding='utf-8'
大文件處理使用流式下載與分塊處理

6.2 完整異常處理框架

def safe_fetch(url, max_retries=3):
    for _ in range(max_retries):
        try:
            headers = {'User-Agent': 'Mozilla/5.0'}
            response = requests.get(url, headers=headers, timeout=10)
            response.raise_for_status()
            response.encoding = 'utf-8'
            return response.content
        except requests.exceptions.RequestException as e:
            print(f"請求失敗: {e}")
            continue
    raise RuntimeError(f"超過最大重試次數(shù): {url}")

七、總結(jié)與擴展

7.1 技術(shù)價值

  • 知識管理:將碎片化網(wǎng)頁內(nèi)容系統(tǒng)化保存
  • 閱讀體驗:EPUB格式支持字體調(diào)整、書簽、夜間模式等
  • 跨平臺:生成的電子書可在Kindle、iOS/Android設(shè)備無縫閱讀

7.2 擴展方向

  • 自動化爬蟲:結(jié)合Scrapy框架實現(xiàn)大規(guī)模內(nèi)容抓取
  • 增強排版:使用WeasyPrint將HTML轉(zhuǎn)為PDF
  • 云存儲:集成AWS S3或阿里云OSS實現(xiàn)自動備份
  • AI增強:通過NLP技術(shù)自動生成摘要與關(guān)鍵詞

通過本文介紹的技術(shù)方案,讀者可以輕松構(gòu)建自己的網(wǎng)頁內(nèi)容保存系統(tǒng)。無論是簡單的TXT備份,還是專業(yè)的EPUB電子書制作,Python都能提供高效可靠的解決方案。實際開發(fā)中,建議根據(jù)具體需求選擇合適的技術(shù)組合,并始終遵守目標網(wǎng)站的robots.txt協(xié)議與版權(quán)法規(guī)。

到此這篇關(guān)于Python實現(xiàn)網(wǎng)頁內(nèi)容轉(zhuǎn)純文本與EPUB電子書的完整指南的文章就介紹到這了,更多相關(guān)Python網(wǎng)頁內(nèi)容轉(zhuǎn)文本與EPUB內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python多進程編程技術(shù)實例分析

    Python多進程編程技術(shù)實例分析

    這篇文章主要介紹了Python多進程編程技術(shù),包括了線程、隊列、同步等概念及相關(guān)的技巧總結(jié),需要的朋友可以參考下
    2014-09-09
  • Python可視化之pyechart庫使用詳解

    Python可視化之pyechart庫使用詳解

    這篇文章主要介紹了Python可視化之pyechart庫使用詳解,Pyecharts 提供了一個簡單而直觀的 API 接口,使得使用者無需了解復雜的 JavaScript 語法,即可通過 Python 代碼實現(xiàn)高度定制化的圖表設(shè)計,需要的朋友可以參考下
    2023-12-12
  • python列表刪除和多重循環(huán)退出原理詳解

    python列表刪除和多重循環(huán)退出原理詳解

    這篇文章主要介紹了python列表刪除和多重循環(huán)退出原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • django-filter和普通查詢的例子

    django-filter和普通查詢的例子

    今天小編就為大家分享一篇django-filter和普通查詢的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python中的異常處理簡明介紹

    Python中的異常處理簡明介紹

    這篇文章主要介紹了Python中的異常處理簡明介紹,本文講解了try-except檢測異常、上下文管理器(with…as…語句)、raise引發(fā)異常、斷言等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • python3+PyQt5自定義視圖詳解

    python3+PyQt5自定義視圖詳解

    這篇文章主要為大家詳細介紹了python3+PyQt5自定義視圖的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python中的項目目錄結(jié)構(gòu)

    python中的項目目錄結(jié)構(gòu)

    這篇文章主要介紹了python中的項目目錄結(jié)構(gòu),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python Word實現(xiàn)批量替換文本并生成副本

    Python Word實現(xiàn)批量替換文本并生成副本

    這篇文章主要為大家詳細介紹了Python Word如何實現(xiàn)批量替換文本并生成副本,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-10-10
  • python包導入的兩種方式

    python包導入的兩種方式

    本文主要介紹了python包導入的方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • 使用Python創(chuàng)建和定制SmartArt圖形的示例代碼

    使用Python創(chuàng)建和定制SmartArt圖形的示例代碼

    在制作專業(yè)的PowerPoint演示文稿時,SmartArt圖形是一種強大的可視化工具,能夠?qū)碗s的信息、流程或?qū)哟谓Y(jié)構(gòu)以直觀的方式呈現(xiàn),本文將深入探討如何使用Python在PowerPoint演示文稿中 programmatically創(chuàng)建、定制和操作SmartArt圖形,需要的朋友可以參考下
    2026-04-04

最新評論

休宁县| 汉川市| 容城县| 凭祥市| 甘孜| 沙坪坝区| 西贡区| 永泰县| 镇江市| 六盘水市| 兴化市| 准格尔旗| 鄢陵县| 余江县| 山阳县| 木里| 四子王旗| 阿勒泰市| 陇西县| 大悟县| 安宁市| 兰州市| 庄浪县| 宜宾市| 玉林市| 泸定县| 海南省| 湘潭县| 吐鲁番市| 崇州市| 民权县| 和顺县| 长垣县| 阜新市| 黄骅市| 介休市| 资溪县| 浪卡子县| 贞丰县| 乌拉特前旗| 兴山县|