0

我有这样的 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'`
4

1 回答 1

2

而是寻找<principal>元素:

for principal in soup.findAll('principal'):
    login = principal.find('login')
    if login is None:
        continue
    print principal['principal-id'], login.string
于 2012-11-13T08:11:17.030 回答