-1

使用 urllib2.Request(url) 和 BeautifulSoup 返回以下内容:

<?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>

这就是我要返回您在上面看到的列表的方法:

req = urllib2.Request(url)
response = urllib2.urlopen(req)
userlist = response.read()
soup = BeautifulSoup(userlist)
print(soup.prettify())

我想打电话username = sys.argv[1]并让它返回principal-id包含username<login> </login>

4

2 回答 2

2

嗯……听起来很有趣。

尝试这样的事情:

req = urllib2.Request(url)
response = urllib2.urlopen(req)
userlist = response.read()
soup = BeautifulSoup(userlist)

username = sys.argv[1]

def get_id(username_var):
    tags = soup.findAll('login')
    for data in tags:
        if str(data.contents[0]).strip() != username_var:
            continue
        print data.parent['account-id']

get_id(username)

这应该返回与提供的用户名关联的帐户 ID。

于 2012-11-13T22:08:17.010 回答
2

也许尝试查看解析 html 的BeautifulSoup

于 2012-11-13T06:42:54.093 回答