【问题标题】:how to parse text from each chapter in epub?如何解析epub中每一章的文本?
【发布时间】:2019-06-01 21:28:28
【问题描述】:

我正在尝试将书籍中的内容从 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 是我正在尝试使用的功能,目前它适用于前几章,然后开始失败。我只是希望能够将每章中的所有文本放入相应的列表中。非常感谢。我将继续努力。如果您能提供任何帮助或建议,将不胜感激。

【问题讨论】:

    标签: python parsing lxml epub


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多