0

I ran the same exact format for a different website scraper and that worked but for some reason I keep getting an error message for this function. "UnboundLocalError: local variable 'bs' referenced before assignment".

I tried to assign a global variable to bs (the dataframe), the code works but doesn't return anything. I also have tried nested functions but still no luck. This is the 4th time I have used this function format and the other three times they have worked without ever producing the error. Any help would be greatly appreciated.

def barstool(website):
browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get(website)
matches = browser.find_elements_by_class_name('basic-event-row')
sleep(10)
info = []
for match in matches:
    info.append(match.text)
y = []
for x in info:
    y.append(x.split('\n'))
master = []
for x in y:
    for z in x:
        if z == '- -':
            z == 'N/A'
        master.append(z)

removes = ('TODAY', 'TOMORROW', 'FRI', 'SAT', 'SUN', 'MON', 'TUE', 'WED', 'THUR', 'SPREA', 'TOTA', 'More', 'Bets')
for x in range(len(master))[::-1]:
    if master[x].startswith(removes):
        master.remove(master[x])
    else:
        pass
for x in range(len(master)):
    if master[x] == '- -':
        master.insert(x+1, None)
    else:
        pass

dic = {}
for x in range(len(master))[0:13:14]:
    dic['Away Team'] = master[x]
    dic['Home Team'] = master[x+1]
    dic['Away Spread'] = master[x+2]
    dic['Away Spread Odds'] = master[x+3]
    dic['Home Spread'] = master[x+4]
    dic['Home Spread Odds'] = master[x+5]
    dic['team_delete'] = master[x+6]
    dic['Away Moneyline'] = master[x+7]
    dic['team2_delete'] = master[x+8]
    dic['Home Moneyline'] = master[x+9]
    dic['Over'] = master[x+10]
    dic['Over Odds'] = master[x+11]
    dic['Under'] = master[x+12]
    dic['Under Odds'] = master[x+13]
    bs = pd.DataFrame(dic, index=[0])

for x in range(len(master))[14::14]:
    dic = {}
    dic['Away Team'] = master[x]
    dic['Home Team'] = master[x+1]
    dic['Away Spread'] = master[x+2]
    dic['Away Spread Odds'] = master[x+3]
    dic['Home Spread'] = master[x+4]
    dic['Home Spread Odds'] = master[x+5]
    dic['team_delete'] = master[x+6]
    dic['Away Moneyline'] = master[x+7]
    dic['team2_delete'] = master[x+8]
    dic['Home Moneyline'] = master[x+9]
    dic['Over'] = master[x+10]
    dic['Over Odds'] = master[x+11]
    dic['Under'] = master[x+12]
    dic['Under Odds'] = master[x+13]
    bs2 = pd.DataFrame(dic, index=[0])
    bs = bs.append(bs2) 
return bs
  • Where do you declare a `bs` variable? How would you assign something to `bs`, if there was never a `bs` to begin with (as in the case of the first loop never having an iteration). In other words, if I say `x = x + 5`, but never declared `x`, what would the new value be? – Rogue Sep 13 '22 at 23:02
  • 1
    Does this answer your question? [Python 3: UnboundLocalError: local variable referenced before assignment](https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment) – Nick Sep 13 '22 at 23:19
  • All the answers I see in that thread are to assign a global variable. Unfortunately when I assign global bs at the start of the function, this runs without error, but doesn't create bs dataframe of the dictionary I loop through – Steve Hydzik Sep 14 '22 at 16:50
  • @Rogue but don't I assign bs in the second to last for loop by creating the first dataframe with it? – Steve Hydzik Sep 14 '22 at 17:23
  • @SteveHydzik if that second-to-last for loop doesn't have an iteration occur, then the last for loop will use `bs` even if it was never assigned to. – Rogue Sep 14 '22 at 23:59

0 Answers0