0

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)
clay
  • 29
  • 2

2 Answers2

1

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

CountZer0
  • 11
  • 4
  • [Please do not post an answer that consists essentially of a link](https://stackoverflow.com/questions/how-to-answer). Please [edit] your answer to include relevant content in your answer and leave the link for extra information or as a reference. – ljmc Sep 25 '22 at 10:08
0

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.

MD Kawsar
  • 313
  • 2
  • 12
  • I tried playing around with that I think it might work. Is there a way to remove the URL that is causing the error, and then rerun my function? – clay Sep 19 '22 at 19:37
  • I know that certain parts are going to fail, and want to remove them and run it again with the ones that don't fail. – clay Sep 19 '22 at 19:38