0

I'm trying to scrape web data, creating 30 data frames. The following code doesn't work:

#import time & pandas
import time
import pandas as pd
franchises = {'atl':'ATL', 'bos':'BOS', 'brk':'BRK', 'chi':'CHI', 'cho':'CHO', 'cle':'CLE', 'dal':'DAL', 'den':'DEN', 'det':'DET', 'gsw':'GSW', 'hou':'HOU', 'ind':'IND', 'lac':'LAC', 'lal':'LAL', 'mem':'MEM', 'mia':'MIA', 'mil':'MIL', 'min':'MIN', 'nop':'NOP', 'nyk':'NYK', 'okc':'OKC', 'orl':'ORL', 'phi':'PHI', 'pho':'PHO', 'por':'POR', 'sac':'SAC', 'sas':'SAS', 'tor':'TOR', 'uta':'UTA', 'was':'WAS'}

#set up custom function to scrape contract dataframes from BB-Ref
def dfscrape(tm_nm):
    url = 'https://www.basketball-reference.com/contracts/' + franchises[tm_nm] + '.html'

    contracts = pd.read_html(url)[0]
    time.sleep(1)
    return contracts

dfscrape(tm_nm = 'atl')

The code to assign the URL works. However the dataframe 'contracts' is not always created when I run dfscrape(tm_nm = 'atl'). Additionally, I would like to change the name of "contracts" over each instance of the function so that I have 30 dataframes.

Should I be using a for loop? I can't figure out how to iteratively assign new names to dataframes.

bhbennett3
  • 123
  • 8
  • 1
    *to change the name of "contracts"* - to what name? – RomanPerekhrest Oct 11 '19 at 20:22
  • @RomanPerekhrest ideally I'd like to change it to "ATL_contracts" or "contracts_atl", just something to keep the names unique -> the dataframes distinguishable – bhbennett3 Oct 11 '19 at 20:33
  • You probably want to store this in a dict: https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – ALollz Oct 11 '19 at 20:49

0 Answers0