lxml的Element.getAttribute()方法调用在哪里?

时间:2011-12-27 23:19:16

标签: python lxml

我一直在挖掘'并且挖掘'大约2个小时,但我找不到这个错误的原因:

1) JBoss
2) Tomcat
Select an application: 1
<App id="1" type="JBoss" path="" state="ok"/>

Traceback (most recent call last):
  File "/root/RService.py", line 47, in <module>
    Stats = app.getAttribute('state')
AttributeError: 'lxml.etree._Element' object has no attribute 'getAttribute'

我的部分代码是:

    app_types = set(nXML.xpath('//Group[@id=$gList]//descendant::App/@type',gList=gList))
    app_dict = dict((x+1,y) for (x,y) in enumerate(app_types))

    print "\n".join("%d) %s" % x for x in sorted(app_dict.items()))
    app_selection = input("Select an application: ")
    app_type = app_dict.get(int(app_selection))

    for app in nXML.xpath('//Group[@id=$gList]//App[@type=$app_type]',gList=gList,app_type=app_type):
        print etree.tostring(app)

        Stats = app.getAttribute('state')
        stype = app.getAttribute('type')
        trigger = 0

        while trigger < 1:
            if Stats == 'ok':
                try: ... ...

以前工作过,语法怎么了?

由于

1 个答案:

答案 0 :(得分:1)

尝试app.get('state')。这适用于LXML 2.2.7。