簡體   English   中英

Python NBA統計

[英]Python NBA stats

我正在嘗試在以下XML http://api.cbssports.com/fantasy/stats?version=3.0&timeframe=2014&period=ytd&SPORT=basketball中為每個玩家打印玩家ID和PPG。

但是,當我打印時,什么也沒打印,我也不知道為什么:

from urllib2 import Request, urlopen, URLError
import xml.etree.ElementTree as ET

request = Request('http://api.cbssports.com/fantasy/stats?version=3.0&timeframe=2014&period=ytd&SPORT=basketball')

try:
    response = urlopen(request)
    tree = ET.parse(response)
    root = tree.getroot()
    for stats in root.findall('.//player_stats/stats'):
        id = stats.get('player_id')
        PPG = stats.get('stat abbr="PPG"')
        print id, PPG
except URLError, e:
    print 'error:', e

stats不是player_stats的直接player_stats

相反,遍歷player節點,從attrib字典中獲取id 為了找到PPG值,請使用findtext()

for stats in root.findall('.//player_stats/player'):
    id = stats.attrib.get('id')
    PPG = stats.findtext('.//stat[@abbr="PPG"]')
    print id, PPG

印刷品:

1992786 24.6
307818 12.2615384615385
555968 12.375
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM