如果下面的脚本已经运行,它会从我想要的所有人开始(例如:Nick Ahmed,Alex Avila等)。但是在9个左右之后给我一个KeyError: 'row'
。
SCRIPT
import csv
import requests
import datetime
from pprint import pprint
# GET TEAM ID AND STORE AS team_id
req = requests.get('http://lookup-service-prod.mlb.com/json/named.team_all_season.bam?sport_code=%27mlb%27&all_star_sw=%27N%27&sort_order=name_asc&season=%272018%27')
data = req.json()['team_all_season']['queryResults']['row']
for teams in data:
team_id = teams['team_id']
team = teams['name_abbrev']
# GET PLAYER ID using team_id string and store PLAYER ID as playerId
req1 = requests.get('http://lookup-service-prod.mlb.com/json/named.roster_40.bam?team_id=' + str(team_id) + '')
roster = req1.json()['roster_40']['queryResults']['row']
for info in roster:
player = info['name_display_first_last']
playerId = info['player_id']
# Use playerId string to run through PECOTA BATTING PROJECTIONS
req2 = requests.get("http://lookup-service-prod.mlb.com/json/named.proj_pecota_batting.bam?season='2018'&player_id='" + str(playerId) + "'")
info1 = req2.json()['proj_pecota_batting']['queryResults']['row']
# KEYS
hr = info1['hr']
sac = info1['sac']
rbi = info1['rbi']
tb = info1['tb']
slg = info1['slg']
bavg = info1['avg']
bb = info1['bb']
ops = info1['ops']
hbp = info1['hbp']
games = info1['g']
d = info1['d']
e = info1['e']
so = info1['so']
sf = info1['sf']
tpa = info1['tpa']
h = info1['h']
cs = info1['cs']
obp = info1['obp']
t = info1['t']
s = info1['s']
r = info1['r']
sb = info1['sb']
sbpct = info1['sbpct']
ab = info1['ab']
ibb = info1['ibb']
print(player,team,hr,sac,rbi,tb,slg,bavg,bb,ops,hbp,games,d,e,so,sf,tpa,h,cs,obp,t,s,r,sb,sbpct,ab,ibb)
如果我添加try:
except(KeyError):
它会起作用,但会在此过程中跳过很多玩家,就像我之前使用的示例一样。这对我没有意义,因为如果他们在try
之前工作,他们为什么不现在呢?仅复制以下脚本的已编辑部分,以显示try:
except:
。
脚本编辑:
# Use playerId string to run through PECOTA BATTING PROJECTIONS
req2 = requests.get("http://lookup-service-prod.mlb.com/json/named.proj_pecota_batting.bam?season='2018'&player_id='" + str(playerId) + "'")
try:
info1 = req2.json()['proj_pecota_batting']['queryResults']['row']
except(KeyError):
# KEYS
hr = info1['hr']
sac = info1['sac']
rbi = info1['rbi']
tb = info1['tb']
slg = info1['slg']
bavg = info1['avg']
bb = info1['bb']
ops = info1['ops']
hbp = info1['hbp']
games = info1['g']
d = info1['d']
e = info1['e']
so = info1['so']
sf = info1['sf']
tpa = info1['tpa']
h = info1['h']
cs = info1['cs']
obp = info1['obp']
t = info1['t']
s = info1['s']
r = info1['r']
sb = info1['sb']
sbpct = info1['sbpct']
ab = info1['ab']
ibb = info1['ibb']
print(player,team,hr,sac,rbi,tb,slg,bavg,bb,ops,hbp,games,d,e,so,sf,tpa,h,cs,obp,t,s,r,sb,sbpct,ab,ibb)
答案 0 :(得分:1)
以下是try
except
应该如何:
import csv
import requests
import datetime
from pprint import pprint
##outfile = open('MLB_HITTER_PICOTA_' + datetime.datetime.now().strftime('%Y') + '.csv','w',newline='')
##writer = csv.writer(outfile)
##writer.writerow(["mlb_hitter","team","hr","sac","rbi","tb","slg","bavg","bb","ops","hbp","games","d","e","so","sf","tpa","h","cs","obp","t","s","r","sb","sbpct","ab","ibb"])
# GET TEAM ID AND STORE AS team_id
req = requests.get('http://lookup-service-prod.mlb.com/json/named.team_all_season.bam?sport_code=%27mlb%27&all_star_sw=%27N%27&sort_order=name_asc&season=%272018%27')
data = req.json()['team_all_season']['queryResults']['row']
for teams in data:
team_id = teams['team_id']
team = teams['name_abbrev']
# GET PLAYER ID using team_id string and store PLAYER ID as playerId
req1 = requests.get('http://lookup-service-prod.mlb.com/json/named.roster_40.bam?team_id=' + str(team_id) + '')
roster = req1.json()['roster_40']['queryResults']['row']
for info in roster:
player = info['name_display_first_last']
playerId = info['player_id']
# Use playerId string to run through PECOTA BATTING PROJECTIONS
req2 = requests.get("http://lookup-service-prod.mlb.com/json/named.proj_pecota_batting.bam?season='2018'&player_id='" + str(playerId) + "'")
## pprint(req2.text)
try:
info1 = req2.json()['proj_pecota_batting']['queryResults']['row']
hr = info1['hr']
sac = info1['sac']
rbi = info1['rbi']
tb = info1['tb']
slg = info1['slg']
bavg = info1['avg']
bb = info1['bb']
ops = info1['ops']
hbp = info1['hbp']
games = info1['g']
d = info1['d']
e = info1['e']
so = info1['so']
sf = info1['sf']
tpa = info1['tpa']
h = info1['h']
cs = info1['cs']
obp = info1['obp']
t = info1['t']
s = info1['s']
r = info1['r']
sb = info1['sb']
sbpct = info1['sbpct']
ab = info1['ab']
ibb = info1['ibb']
print(player,team,hr,sac,rbi,tb,slg,bavg,bb,ops,hbp,games,d,e,so,sf,tpa,h,cs,obp,t,s,r,sb,sbpct,ab,ibb)
except(KeyError):
pass
try:
#如果某些部分不起作用
except():
#在我的案例except(KeyError):
pass
#到下一个