Python xpath not working?

前端 未结 2 2017
北恋
北恋 2021-01-26 08:10

Okay, this is starting to drive me a little bit nuts. I\'ve tried several xml/xpath libraries for Python, and can\'t figure out a simple way to get a stinkin\' \"title\" element

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 08:59

    It is indeed the namespaces. It was a bit tricky to find in the lxml docs, but here's how you do it:

    from lxml import etree
    doc = etree.parse(open('index.html'))
    doc.xpath('//default:title', namespaces={'default':'http://www.w3.org/2005/Atom'})
    

    You can also do this:

    title_finder = etree.ETXPath('//{http://www.w3.org/2005/Atom}title')
    title_finder(doc)
    

    And you'll get the titles back in both cases.

提交回复
热议问题