简体   繁体   中英

Splitting a HTML document using lxml.html

I have a HTML document containing multiple chapters of text where the H1 tag is the chapter separator. How can I split such a document into html snippets where each snippet starts with the h1 tag of the corresponding "chapter". I though of prettifying the HTML and then iterating of the content line by line...but that's kind of a hack. Is there a better solution using lxml?

tree = lxml.html.document_fromstring(htmltext)
for element in tree.iter():
  if element.tag == 'h1':
    for subelement in element:
      // do stuff

That'll find the elements that are h1 tags and then you can iterate through all its subelements. You could also just take all the text inside the element as a string and do stuff with it that way as well. Whatever you want to do. http://lxml.de/ lxml is awesome and I would recommend it. I had to update code already using it and just kept the website open for reference whenever I had a question :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM