How can I retry url from list if ValueError?
Error:
JavaScript
x
3
1
raise ValueError("No tables found")
2
ValueError: No tables found
3
Or other exceptions.
Can use if ValueError
then driver.refresh(2wice)
but I dont know location in the code:
JavaScript
1
14
14
1
if __name__ == '__main__':
2
3
results = None
4
5
for url in urls:
6
game_data = parse_data(url)
7
if game_data is None:
8
continue
9
result = pd.DataFrame(game_data.__dict__)
10
if results is None:
11
results = result
12
else:
13
results = results.append(result, ignore_index=True)
14
Maximum retry 2
Advertisement
Answer
You can put the entire thing in a try/except block and if it encounters a ValueError at some point, you can put the same code under except and it will retry it.
JavaScript
1
24
24
1
if __name__ == '__main__':
2
3
results = None
4
5
for url in urls:
6
try:
7
game_data = parse_data(url)
8
if game_data is None:
9
continue
10
result = pd.DataFrame(game_data.__dict__)
11
if results is None:
12
results = result
13
else:
14
results = results.append(result, ignore_index=True)
15
except ValueError:
16
game_data = parse_data(url)
17
if game_data is None:
18
continue
19
result = pd.DataFrame(game_data.__dict__)
20
if results is None:
21
results = result
22
else:
23
results = results.append(result, ignore_index=True)
24
Throw in a finally: after the except block with the same spacing if you want it to do something after it’s tried twice.