开发者

Xpath doesn't match

开发者 https://www.devze.com 2023-03-29 05:51 出处:网络
I\'m trying to get some elements from a page. Unfortunatel开发者_开发知识库y it results with an empty list. The pretty-printed tree includes this element:

I'm trying to get some elements from a page. Unfortunatel开发者_开发知识库y it results with an empty list. The pretty-printed tree includes this element:

<html:a title="..." href="..." id="..." class="topic_title">...</html:a>

However when I do this on the same tree:

page.xpath('''.//a[@class="topic_title"]''')

I get an empty list. The tree was created with html5lib / lxml treebuilder.


It seems as if you are dealing with XHTML, so you could register the namespace html before evaluating the XPath expression:

page.xpath('''.//html:a[@class="topic_title"]''',
           namespaces={'html': 'http://www.w3.org/1999/xhtml'})

See also Namespaces and Prefixes:

If your XPath expression uses namespace prefixes, you must define them in a prefix mapping. To this end, pass a dictionary to the namespaces keyword argument that maps the namespace prefixes used in the XPath expression to namespace URIs.

0

精彩评论

暂无评论...
验证码 换一张
取 消