简体   繁体   中英

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. From the Python script below, the "print(response.content)" lists contents ok and includes: '''

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

''' And prints child tags 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' '''

And does not print anything from the for condition below: ''' for category in root.findall('''.//@term="changetimber/hasgame" ] '''): print(category.attrib) '''

The Python script below is: '''

    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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