Объект xml.etree.ElementTree.Element не имеет атрибута getchildren, а относительный XPath не находит атрибуты.Python

Программы на Python
Ответить
Anonymous
 Объект xml.etree.ElementTree.Element не имеет атрибута getchildren, а относительный XPath не находит атрибуты.

Сообщение Anonymous »

Я не могу анализировать и распечатывать элементы и атрибуты из категории коллекции с веб-страницы с помощью атома-xml. В приведенном ниже скрипте Python «print(response.content)» отображает содержимое в порядке и включает:
'''

Код: Выделить всё

"xmlns= 
'''
И печатает дочерние теги нормально:
'''

Код: Выделить всё

{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' не имеет атрибута
'getchildren'
'''
И не печатает ничего из приведенного ниже условия for:
'''
для категории в 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)
'''

Подробнее здесь: https://stackoverflow.com/questions/722 ... nd-relativ
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»