我正在尝试打印此XML http://api.cbssports.com/fantasy/players/list?version=3.0&SPORT=basketball
中每个玩家的名字,姓氏,蛋白质和标题这是我的代码到目前为止,但它没有工作
from urllib2 import Request, urlopen, URLError
import xml.etree.ElementTree as ET
request = Request('http://api.cbssports.com/fantasy/players/list?version=3.0&SPORT=basketball')
try:
response = urlopen(request)
tree = ET.parse(response)
root = tree.getroot()
for player in root.findall('.//players'):
firstname = player.find('firstname').text
lastname = player.find('lastname').text
print firstname, lastname
except URLError, e:
print 'error:', e
我认为错误发生在for循环语句中,因为firstname不是玩家的直接子,其父级是玩家ID
答案 0 :(得分:1)
预先设置一个双斜杠来搜索上下文中当前元素的任何位置。您需要迭代player
元素而不是players
:
for player in root.findall('.//players/player'):
firstname = player.findtext('.//firstname')
lastname = player.findtext('.//lastname')