无法在objectify.parse结果上调用lxml.etree._ElementTree.find()

时间:2011-09-08 16:10:01

标签: python lxml

>>> from lxml import objectify
>>> from StringIO import StringIO
>>> f = StringIO("<root>data</root>")
>>> tree = objectify.parse(f)
>>> type(tree)
<type 'lxml.etree._ElementTree'>
>>> tree.find('root')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lxml.etree.pyx", line 1944, in lxml.etree._ElementTree.find (src/lxml/lxml.etree.c:45105)
TypeError: find() takes exactly one argument (2 given)
>>> tree.find()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lxml.etree.pyx", line 1926, in lxml.etree._ElementTree.find (src/lxml/lxml.etree.c:44970)
TypeError: find() takes at least 1 positional argument (0 given)
>>> print tree.find.__doc__
find(self, path, namespaces=None)

        Finds the first toplevel element with given tag.  Same as
        ``tree.getroot().find(path)``.

        The optional ``namespaces`` argument accepts a
        prefix-to-namespace mapping that allows the usage of XPath
        prefixes in the path expression.

请注意,tree.getroot().find有效且find适用于_ElementTree创建的etree.parse个实例。

主要问题:同样的方法如何引发这两个相互排斥的异常?另外,虽然我可以使用tree.getroot().find,但如果它有记录的话,那么较短的形式将是首选,所以我很好奇,它是一个lxml错误吗?

1 个答案:

答案 0 :(得分:0)

我们通过查看相应的来源(长期OSS)来解决这个谜团:

def find(self, path, namespaces=None):
    u"""find(self, path, namespaces=None)

    Finds the first toplevel element with given tag.  Same as
    ``tree.getroot().find(path)``.

    The optional ``namespaces`` argument accepts a
    prefix-to-namespace mapping that allows the usage of XPath
    prefixes in the path expression.
    """
    self._assertHasRoot()
    root = self.getroot()
    if _isString(path):
        start = path[:1]
        if start == u"/":
            path = u"." + path
        elif start == b"/":
            path = b"." + path
    return root.find(path, namespaces)

"lxml.etree.pyx", line 1926是第一个,"lxml.etree.pyx", line 1944是该代码段的最后一行,因此实际上有两种不同的find方法。显然客观化构造了一些不同的对象(因此这是lxml中的一个错误),它不接受namespaces参数。如果您使用lxml.etree.parse来解析StringIO对象,则API工作正常。