我想把epub转换成txt。我首先通过zipfile将epub转换为xhtml。然后我试着把xhtml转换成epub。
但是,由于本地文件名存在问题。例如,我的xhtml文件名是"C:\Users\abc.xhtml",而不是"HTTPS“。好美的汤不起作用。
我该如何解决这个问题呢?
'''
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)发布于 2020-02-29 22:52:04
提取时不需要使用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 发布于 2020-02-29 22:42:24
BeautifulSoup构造函数需要html文件的实际内容,而不是url。试试这个:
with open(html) as f:
contents = f.read()
soup = BeautifulSoupd(contents, 'lxml')https://stackoverflow.com/questions/60466129
复制相似问题