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

Ben*_*n Q 10 python api

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

"xmlns= <atom:category scheme="http://www.zooo.com/types" 
term="changetimber/has-game"/>
Run Code Online (Sandbox Code Playgroud)

''' 并打印子标签:'''

{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
Run Code Online (Sandbox Code Playgroud)

'''

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

并且不会打印以下 for 条件中的任何内容: ''' forcategory 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)
Run Code Online (Sandbox Code Playgroud)

'''

nas*_*jai 14

变成hasgame.getchildren()list(hasgame)

  • 只是为了在此处添加更多有关原因的信息:xml.etree.ElementTree.Element 曾经有一个名为 getchildren() 的方法,因此您将看到使用它的现有代码。它在版本 3.2 中已弃用,然后在 3.9 中删除(请参阅 https://docs.python.org/3.8/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.getchildren) (4认同)