方法对象不是可下载的Python

时间:2017-12-17 05:40:08

标签: python

跑进TypeError: 'method' object is not subscriptable

import csv
import requests
import json

req = requests.get(url="https://www.draftkings.com/lobby/getcontests?sport=NHL")
req.raise_for_status()
data = req.json()

for i, contest in enumerate(data['DraftGroups']):
    draftgroupid = contest['DraftGroupId']
    req2 = requests.get(url="https://api.draftkings.com/draftgroups/v1/draftgroups/" + str(draftgroupid) + "/draftables?format=json")
    data2 = req2.json

    for i, player_info in enumerate(data2['draftables'][0]):
        date = player_info['competition']['startTime']
        print(date)

草案组的输出:

16901
16905
16902
16903

我想知道它是否可能是第二个while循环?

1 个答案:

答案 0 :(得分:0)

你应该调用json方法,所以你的代码应该改变如下。

import csv
import requests
import json

req = requests.get(url="https://www.draftkings.com/lobby/getcontests?sport=NHL")
req.raise_for_status()
data = req.json()

for i, contest in enumerate(data['DraftGroups']):
    draftgroupid = contest['DraftGroupId']
    req2 = requests.get(url="https://api.draftkings.com/draftgroups/v1/draftgroups/" + str(draftgroupid) + "/draftables?format=json")
    data2 = req2.json() # Note that this line was changed

    for i, player_info in enumerate(data2['draftables']): # Removed [0]
        date = player_info['competition']['startTime']
        print(date)