Python實現(xiàn)網(wǎng)頁內(nèi)容轉(zhuǎn)純文本與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(可選,應對JavaScript渲染)
1.2 環(huán)境配置
pip install requests beautifulsoup4 lxml ebooklib selenium
注:若需處理動 態(tài)網(wǎng)頁,還需下載對應瀏覽器的WebDriver(如ChromeDriver)
二、網(wǎng)頁內(nèi)容提取技術(shù)
2.1 基礎抓取流程
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]) # 預覽前200字符
三、純文本保存方案
3.1 基礎實現(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 基礎生成代碼
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)
# 設置目錄
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')
# 假設每章標題在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 常見問題解決方案
| 問題類型 | 解決方案 |
|---|---|
| 反爬機制 | 設置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設備無縫閱讀
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)文章
pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作
這篇文章主要介紹了pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05

