Why doesn't xpath work when processing an XHTML document with lxml (in python)?

前端 未结 3 890
后悔当初
后悔当初 2020-12-03 07:35

I am testing against the following test document:




        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 07:42

    The problem is the namespaces. When parsed as XML, the img tag is in the http://www.w3.org/1999/xhtml namespace since that is the default namespace for the element. You are asking for the img tag in no namespace.

    Try this:

    >>> tree.getroot().xpath(
    ...     "//xhtml:img", 
    ...     namespaces={'xhtml':'http://www.w3.org/1999/xhtml'}
    ...     )
    []
    

提交回复
热议问题