繁体   English   中英

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

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

我无法使用 atom-xml 从网页的集合类别中解析和打印元素和属性。 从下面的 Python 脚本中,“print(response.content)”列出的内容没问题,包括:'''

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

''' 并打印子标签 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

'''

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

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

下面的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM