我正在尝试抓取游戏的结果,但是有一个游戏进行了第三轮比赛,而有一个游戏没有进行比赛,因此在没有第三轮比赛的游戏中会发生错误,并且不要刮任何东西。
IndexError: list index out of range
部分代码:
player1_game1 = response.css('tbody tr:nth-child(1) td span::text').re('\d')[0]
player2_game1 = response.css('tbody tr:nth-child(2) td span::text').re('\d')[0]
player1_game2 = response.css('tbody tr:nth-child(1) td span::text').re('\d')[1]
player2_game2 = response.css('tbody tr:nth-child(2) td span::text').re('\d')[1]
player1_game3 = response.css('tbody tr:nth-child(1) td span::text').re('\d')[2]
player2_game3 = response.css('tbody tr:nth-child(2) td span::text').re('\d')[2]
items['player1_game1'] = player1_game1
items['player2_game1'] = player2_game1
items['player1_game2'] = player1_game2
items['player2_game2'] = player2_game2
items['player1_game3'] = player1_game3
items['player2_game3'] = player2_game3
如果我没有第三局,我什么也不想回答,将其留空,但将其余部分刮掉
错误:
IndexError: list index out of range
如何解决此问题并产生正确的结果?
答案 0 :(得分:0)
您可以使用try...except
块:
player1_game1 = response.css('tbody tr:nth-child(1) td span::text').re('\d')[0]
player2_game1 = response.css('tbody tr:nth-child(2) td span::text').re('\d')[0]
player1_game2 = response.css('tbody tr:nth-child(1) td span::text').re('\d')[1]
player2_game2 = response.css('tbody tr:nth-child(2) td span::text').re('\d')[1]
try:
player1_game3 = response.css('tbody tr:nth-child(1) td span::text').re('\d')[2]
player2_game3 = response.css('tbody tr:nth-child(2) td span::text').re('\d')[2]
except:
player1_game3 = ""
player2_game3 = ""