[英]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.