Python lxml XPath problem

前端 未结 2 672
盖世英雄少女心
盖世英雄少女心 2020-12-21 00:49

I\'m trying to print/save a certain element\'s HTML from a web-page.
I\'ve retrieved the requested element\'s XPath from firebug.

All I wish is to save this ele

相关标签:
2条回答
  • 2020-12-21 01:34

    Your XPath is obviously a bit too long, why don't you try shorter ones and see if they match. One problem might be "tbody" which gets automatically created in the DOM by browsers but the HTML markup usually does not contain it.

    Here's an example of how to use XPath results:

    >>> from lxml import etree
    >>> from StringIO import StringIO
    >>> doc = etree.parse(StringIO("<html><body>a<something/>b</body></root>"), etree.HTMLParser())
    >>> doc.xpath("/html/body/text()")
    ['a', 'b']
    

    So you could just "".join(...) all text parts together if needed.

    0 讨论(0)
  • 2020-12-21 01:37

    Not sure I completely follow what you are trying to accomplish, but ultimately I think you are looking for:

    print etree.tostring(elem[0])
    
    0 讨论(0)
提交回复
热议问题