Zacharia2 / SuperMemo-Toolkit

SuperMemo 增强工具(CLI命令行)。包含图链整理、EPUB图书转换导入、Latex公式转图片等。
GNU General Public License v2.0
24 stars 3 forks source link

memo #1

Closed Zacharia2 closed 10 months ago

Zacharia2 commented 10 months ago

isinstance(chapter, tuple):

是元组的时候就说明是有子集的数据。元组的第一个元素是本层的数据,第二个元素是下一层的数据,也是入口。

Zacharia2 commented 10 months ago

简单来说,用Python解析epub格式的电子书并提取需要的数据,仅仅需要两步:

使用Ebooklib打开epub文件,提取文本内容; 使用Beautiful Soup解析文本内容,提取数据。

  1. 用到的库 使用pip安装Ebooklib和Beautiful Soup

pip install EbookLib pip install beautifulsoup4

  1. 方法 以提取电子书中的文本数据为例。

载入电子书

book = epub.read_epub(book_path)

解析

for item in book.get_items():

提取书中的文本内容

if item.get_type() == ebooklib.ITEM_DOCUMENT:
    # epub中的内容是html格式,使用BeautifulSoup可以完美解析
    soup = BeautifulSoup(item.get_content(), 'html')

接下来就可以使用BeautifulSoup去解析内容,提取需要的数据。

Ebooklib的更多用法见官方文档:

http://docs.sourcefabric.org/projects/ebooklib/en/latest/tutorial.html

BeautifulSoup官方文档:

https://beautifulsoup.cn/

Zacharia2 commented 10 months ago

https://deepinout.com/beautifulsoup/beautifulsoup-questions/495_beautifulsoup_how_to_split_a_html_page_to_multiple_pages_using_python_and_beautiful_soup.html

Zacharia2 commented 10 months ago

锚点:href:'Text/Section0001_0012.xhtml#toc_1'

这样的就获取不到。

在书中,其实有这个页,Text/Section0001_0012.xhtml。但因为锚点,ebook没获取到。

toc_1 指向的是 id,就是Text/Section0001_0012.xhtml这个文档的名为toc_1的id。

Zacharia2 commented 10 months ago

ok