如何解析epub中每个章节的文字?

时间:2019-06-01 21:28:29

标签: python parsing lxml epub

我正在尝试将书籍中的内容从epub格式解析并转换为自己的结构,但是在检测和提取每章之间的所有文本时遇到了麻烦,我该如何完成呢?

这是我要使用的两个epub文件,最终要在其他文件上使用:http://www.gutenberg.org/ebooks/11.epub.noimages?session_id=f5b366deca86ee5e978d79f53f4fcaf1e0ac32ca

http://www.gutenberg.org/ebooks/98.epub.noimages?session_id=f5b366deca86ee5e978d79f53f4fcaf1e0ac32ca

我能够像这样将每个章节的标题放入字典:

{'ALICE’S ADVENTURES IN WONDERLAND': [], 'THE MILLENNIUM FULCRUM EDITION 3.0': [], 'Contents': [], 'CHAPTER I. Down the Rabbit-Hole': [], 'CHAPTER II. The Pool of Tears': [], 'CHAPTER III. A Caucus-Race and a Long Tale': [], 'CHAPTER IV. The Rabbit Sends in a Little Bill': [], 'CHAPTER V. Advice from a Caterpillar': [], 'CHAPTER VI. Pig and Pepper': [], 'CHAPTER VII. A Mad Tea-Party': [], 'CHAPTER VIII. The Queen’s Croquet-Ground': [], 'CHAPTER IX. The Mock Turtle’s Story': [], 'CHAPTER X. The Lobster Quadrille': [], 'CHAPTER XI. Who Stole the Tarts?': [], 'CHAPTER XII. Alice’s Evidence': []}

我想将每个章节之间的文本都放入该列表中,但是我遇到了很多麻烦

这是我获取本章的方式:

import sys
import lxml
import ebooklib
from ebooklib import epub
from ebooklib.utils import debug
from lxml import etree
from io import StringIO, BytesIO
import csv, json

bookJSON = {}
chapterNav = {}
chapterTitle = {}
chapterCont = {}
def parseNAV(xml):
    """
    Parse the xml
    """

    root = etree.fromstring(xml)

    for appt in root.getchildren():
        for elem in appt.getchildren():
            #print(elem.tag)
            for child in elem.getchildren():
                #print(child.tag)
                if("content" in child.tag):
                    srcTag = child.get("src")
                    #print(child.tag + " src: " + srcTag)
                    contentList = srcTag.split("#")
                    #print(contentList[1])
                    chapterNav[contentList[1]] = text
                    chapterTitle[text.strip()] = []
                    chapterCont[text.strip()] = []
                for node in child.getchildren():
                    if not node.text:
                        text = "None"
                    else:
                        text = node.text
                    #print(node.tag + " => " + text)
            #print(elem.tag + " CLOSED"  + "\n")

def parseContent(xml):
    """
    Parse the xml
    """

    root = etree.fromstring(xml)
    chaptText = []
    chapter= ''
    for appt in root.getchildren():
        for elem in appt.getchildren():
            if(elem.text != None and stringify_children(elem) != None):
                if("h2" in elem.tag):
                    print(stringify_children(elem))
                if (elem.text).strip() in chapterTitle.keys():
                    chapterCont[elem.text.strip()] = chaptText
                    chaptText = []
                else:
                    chaptText.append(stringify_children(elem))
def stringify_children(node):
    return (''.join(node.itertext()).strip()).replace("H2 anchor","")

book = epub.read_epub(sys.argv[1])

# debug(book.metadata)

def getData(id,book,bookJSON):
    data = list(book.get_metadata('DC', id))
    if(len(data) != 0):
        bookJSON[id] = []
        for x in data:
            dataTuple = x
            bookJSON[id].append(str(dataTuple[0]))
        return bookJSON
    return bookJSON


bookJSON =  getData('title',book,bookJSON)
bookJSON = getData('creator',book,bookJSON)
bookJSON = getData('identifier',book,bookJSON)
bookJSON = getData('description',book,bookJSON)
bookJSON = getData('language',book,bookJSON)
bookJSON = getData('subject',book,bookJSON)
nav = list(book.get_items_of_type(ebooklib.ITEM_NAVIGATION))
navXml = etree.XML(nav[0].get_content())
#print(nav[0].get_content().decode("utf-8"))


parseNAV(etree.tostring(navXml))
print(bookJSON)

bookContent = list(book.get_items_of_type(ebooklib.ITEM_DOCUMENT))
for cont in bookContent:
    contentXml = etree.XML(cont.get_content())

    parseContent(etree.tostring(contentXml))
# print(chapterCont)
# print(chapterNav)
# print(chapterTitle)

ParseContent是我要使用的功能,目前它适用于前几章,然后因失败而失败。我只希望能够将每个章节的所有文本输入到相应的列表中。非常感谢你。我将继续努力。如果您能提供任何帮助或建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

找出解决方案,使用章节开头的章节标题创建索引,并将其保存在元组中。然后使用该元组遍历内容并将所有内容附加到相应的章节。希望这有助于下一个希望解析epub的人。如果有人有更好的建议,请告诉我。关于在线epub解析的信息并不多。