简体   繁体   English

xml.etree.ElementTree.Element' 对象没有属性 'getchildren' 并且相对 XPath 找不到属性

[英]xml.etree.ElementTree.Element' object has no attribute 'getchildren' and relative XPath does not find attributes

I am not able parse and print elements and attributes from a collection category from web page using atom-xml.我无法使用 atom-xml 从网页的集合类别中解析和打印元素和属性。 From the Python script below, the "print(response.content)" lists contents ok and includes: '''从下面的 Python 脚本中,“print(response.content)”列出的内容没问题,包括:'''

"xmlns= <atom:category scheme="http://www.zooo.com/types" 
term="changetimber/has-game"/>

''' And prints child tags ok: ''' ''' 并打印子标签 ok: '''

{http://www.w3.org/2007/app}workspace
{http://www.w3.org/2005/Atom}title
{http://www.w3.org/2007/app}collection
{http://www.w3.org/2005/Atom}title
{http://www.w3.org/2007/app}categories
{http://www.w3.org/2005/Atom}category

''' '''

After the Python script py is ran, get error ''' children = hasgame.getchildren AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren' ''' Python脚本py运行后,得到错误''' children = hasgame.getchildren AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getchildren' '''

And does not print anything from the for condition below: ''' for category in root.findall('''.//@term="changetimber/hasgame" ] '''): print(category.attrib) '''并且不打印以下 for 条件中的任何内容: ''' for category in root.findall('''.//@term="changetimber/hasgame" ] '''): print(category.attrib) '''

The Python script below is: '''下面的 Python 脚本是:'''

    import requests
    from requests.auth import HTTPBasicAuth
    import xml.etree.cElementTree as ET
    url = 'http://ipaddress/mygame'
    response = requests.get(url, auth=HTTPBasicAuth('user','pass'))
    print(response)
    print("***********list response content ******************")
    print(response.content)
    print("******** Display child tags********")
    root = ET.fromstring(response.content)
    for child in root.iter('*'):
    print(child.tag)
    # Create a dictionary
    xmlDictionary = {}
    for hasgame in root:
    children = hasgame.getchildren()
    xmlDictionary[children[0].text] = children[1].text

    print("******** print dictionary ******")
    print(xmlDictionary)
    for category in root.findall('''.//@term="changetimber/hasgame" ] '''):
       print(category.attrib)

''' '''

hasgame.getchildren()更改为list(hasgame)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;xml.etree.ElementTree.Element&#39;对象没有属性&#39;encode&#39; - AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'encode' xml.etree.ElementTree.Element&#39; 对象没有属性 &#39;write&#39; - xml.etree.ElementTree.Element' object has no attribute 'write' AttributeError: 'xml.etree.ElementTree.Element' object 没有属性 'children' - AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'children' AttributeError: &#39;xml.etree.ElementTree.Element&#39; 对象没有属性 &#39;_children&#39; -- 不是骗子 - AttributeError: 'xml.etree.ElementTree.Element' object has no attribute '_children' -- not a dupe 为什么bool(xml.etree.ElementTree.Element)的计算结果为False? - Why does bool(xml.etree.ElementTree.Element) evaluate to False? TypeError:'xml.etree.ElementTree.Element'对象不可调用 - TypeError: 'xml.etree.ElementTree.Element' object is not callable Python XML&#39;TypeError:必须为xml.etree.ElementTree.Element,而不是str&#39; - Python XML 'TypeError: must be xml.etree.ElementTree.Element, not str' TypeError:remove() 参数必须是 xml.etree.ElementTree.Element,而不是 dict - TypeError: remove() argument must be xml.etree.ElementTree.Element, not dict TypeError:remove() 参数必须是 xml.etree.ElementTree.Element,而不是 str | 蟒蛇 | xml.etree - TypeError: remove() argument must be xml.etree.ElementTree.Element, not str | python | xml.etree Python XML:&#39;TypeError:SubElement() 参数 1 必须是 xml.etree.ElementTree.Element,而不是 Element&#39; - Python XML: 'TypeError: SubElement() argument 1 must be xml.etree.ElementTree.Element, not Element'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM