python - 如何获取“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)
解决方案
BeautifulSoup
构造函数需要 html 文件的实际内容,而不是 url 。试试这个:
with open(html) as f:
contents = f.read()
soup = BeautifulSoupd(contents, 'lxml')
推荐阅读
- gitlab - 如何在gitlab中更改全局环境变量的值?
- android - 导入aar文件,但有两个图标
- bash - 使用 awk 将 kB 转换为 MB
- git - 是否可以在多台机器之间同步本地 Github 存储库?
- php - 关闭选项卡后更新数据库中的注销时间
- java - How to establish network communication between two Java servers
- javascript - 输入范围滑块拇指js事件
- node.js - 地理定位问题,一旦给出结果,其他所有超时
- python - Facebook fasttext bin 模型 UnicodeDecodeError
- jenkins-pipeline - 如何从詹金斯脚本管道创建属性文件