'''
Код: Выделить всё
"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
Мобильная версия