首页 > 解决方案 > 如何获取“HTTPS”链接以及如何在 python 中将 epub 转换为 txt?

问题描述

我想将 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)

标签: pythonbeautifulsoupepub

解决方案


BeautifulSoup构造函数需要 html 文件的实际内容,而不是 url 。试试这个:

with open(html) as f:
    contents = f.read()
soup = BeautifulSoupd(contents, 'lxml')

推荐阅读