我有这样的XML :(这是汤变量)
<?xml version="1.0" encoding="utf-8"?>
<results>
<status code="ok">
</status>
<principal-list>
<principal account-id="1013353221" has-children="false" is-hidden="false" is-primary="false" principal-id="1013353225" training-group-id="" type="user">
<name>
First Last
</name>
<login>
first.last
</login>
<email>
first.last@domain.com
</email>
<display-uid>
first.last
</display-uid>
</principal>
<principal account-id="1013353221" has-children="false" is-hidden="false" is-primary="false" principal-id="1115269931" training-group-id="" type="user">
<name>
First Last
</name>
<login>
first.last
</login>
<email>
first.last@domain.com
</email>
<display-uid>
first.last
</display-uid>
</principal>
</principal-list>
</results>
我试过这段代码:
loginlist = [el.string for el in soup.findAll('login')]
for entry in loginlist:
print entry
它返回:
first.last
为每个用户。
我还希望找到嵌套principal-id
login
我的输出如下:
`'first.last', '1013353225'`
`'first.last', '1115269931'`
答案 0 :(得分:2)
请改为寻找<principal>
元素:
for principal in soup.findAll('principal'):
login = principal.find('login')
if login is None:
continue
print principal['principal-id'], login.string