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 :)
精彩评论