尝试每天运行并将其另存为带有摆锤的单独CSV文件。现在我只能得到period
的第一天。不确定我是否需要outfile
,但我假设我这样做,因为我希望每个单独的CSV文件都能写入,关闭并启动一个新文件。
import csv
import requests
import datetime
import pendulum
start = pendulum.datetime(2018, 1, 1)
end = pendulum.today()
period = pendulum.period(start, end)
for dt in period.range('days'):
dt.format('YYYY-MM-DD')
break
the_date = dt.format('YYYY-MM-DD')
outfile = open('TEST_PENDULUM_' + str(the_date) + '.csv',"w",newline='')
writer = csv.writer(outfile)
writer.writerow(["Date"])
req = requests.get('https://www.fantasylabs.com/api/lines/4/' + str(the_date) + '/startinggoalies')
data = req.json()['GoalieMatchups']
for teams in data:
HomeTeam = teams['Properties']['EventDate']
print(HomeTeam)
writer.writerow([HomeTeam])
outfile.close()
答案 0 :(得分:2)
您没有在代码上编写迭代逻辑。
import csv
import requests
import datetime
import pendulum
start = pendulum.datetime(2018, 1, 1)
end = pendulum.today()
period = pendulum.period(start, end)
for dt in period.range('days'):
the_date = dt.format('YYYY-MM-DD')
outfile = open('TEST_PENDULUM_' + str(the_date) + '.csv',"w",newline='')
writer = csv.writer(outfile)
writer.writerow(["Date"])
req = requests.get('https://www.fantasylabs.com/api/lines/4/' + str(the_date) + '/startinggoalies')
data = req.json()['GoalieMatchups']
for teams in data:
HomeTeam = teams['Properties']['EventDate']
print(HomeTeam)
writer.writerow([HomeTeam])
outfile.close()