我想将 epub 转换为 txt。 我首先通过 zipfile 将 epub 转换为 xhtml。 然后我尝试通过 beautifulsoup 将 xhtml 转换为 epub。
但是,由于本地文件名存在问题。 例如,我的 xhtml 文件名是“C:\Users\abc.xhtml”,而不是“HTTPS”。 所以 beautifulsoup 不起作用。
我该如何解决这个问题?
'''
import zipfile
zf = zipfile.ZipFile('C:\\Users\\abc.epub')
zf.extractall('C:\\Users\\Desktop\\folder')
'''
import re, requests
from bs4 import BeautifulSoup
html = "C:\\Users\\abc.xhtml"
soup = BeautifulSoup(html, 'lxml')
print(soup.text)
最佳答案
您不需要 BeautifulSoup 来提取。
您可以使用可从 PyPi 安装的 epub-conversion
包将 .epub 文件转换为文本:
pip install epub-conversion
现在从 epub 存档中提取文本是一个简单的任务:
逐行:
from epub_conversion.utils import open_book, convert_epub_to_lines
book = open_book("some_file.epub")
lines = convert_epub_to_lines(book)
现在,正如您的问题一样,您可以将其作为一个整体打印或选择处理每一行:
print(lines)
# or traverse each line
for line in lines:
print(line) # Or do something completely different
关于python - 如何获取 "HTTPS"链接以及如何在 python 中将 epub 转换为 txt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60466129/