Home > Back-end >  How "skip" if error inside of a Python function?
How "skip" if error inside of a Python function?

Time:09-20

I am trying to import tables from multiple pages, and then append each table into one data frame. One of the links does not have a table, which causes the function to not work. Is there a way I can just skip the URLs that result in errors? (There are many more URLs that result in errors that I excluded from this code)

import pandas as pd

    
urls = ['https://basketball.realgm.com/player/Stephen-Curry/GameLogs/1600',
            'https://basketball.realgm.com/player/LeBron-James/GameLogs/250', 
            'https://basketball.realgm.com/player/Anthony-Edwards/GameLogs/117444',
            'https://basketball.realgm.com/player/Jalen-Washington/GameLogs/151233']
    
def fxGameLogs(URL: list) -> pd.DataFrame:
    dfs = [] # empty list
    for x in URL:
        GameLog_list = pd.read_html(x)
        GameLogs = GameLog_list[0]
        dfs.append(GameLogs) # append frame to list        
    return pd.concat(dfs).reset_index(drop=True) # concat frames and return

GameLogs = fxGameLogs(urls)
print(GameLogs)

CodePudding user response:

Maybe you could use a simple if? Or, maybe, use the request function to check for status codes returning before using the site on your for.

Check this out -> Python check if website exists

CodePudding user response:

The try block allows you to test a section of code for faults.
The except block allows you to manage errors.

Example:

def division(num1, num2):
    return num1 / num2

division(1, 0)

if you run this you will get an error like this>>

Traceback (most recent call last):
  File "<string>", line 4, in <module>
File "<string>", line 2, in division
ZeroDivisionError: division by zero

But Using try...except Block code can be run without any error

def division(num1, num2):
    return num1 / num2

try:
    division(1, 0)
except:
    pass

The best practice is to use specific Error Exception:

def division(num1, num2):
    return num1 / num2

try:
    division(1, 0)
except ZeroDivisionError:
    pass

You can also try this:

def division(num1, num2):
    return num1 / num2

try:
    division(1, 0)
except Exception as e:
    print(e)

This will print the Error or can be used in Logging.

  • Related