Search code examples
pythonweb-scrapingnameerror

NameError: name 'container' is not defined Python Webscraping


Im currently trying out webscraping. Everything is fine until the part where I am trying to narrow down the tags to scrape from. Whenever I input the code below, the error above would appear.

I have tried this before however, it worked with the shift of the indents. This time though, it did not work. I have tried to narrow it down through other means eg: by class instead. However, none of it worked this time. Currently stucked.

from bs4 import BeautifulSoup as soup
import requests

link = '*insert link*'
username = 'username123'
password = 'password123'
r = requests.get(link, auth=(username, password))
page = r.content

page_soup = soup(page, "html.parser")

div = page_soup.findAll("div", {"class":"Ovx(s)"})
for table in div:
    tables = table.find("table")
    tbody = tables.find("tbody")
    container = tbody.findAll("tr", {"class":"Bgc($extraLightBlue):h"})

I've tried this as well:

div = page_soup.findAll("div", {"class":"Ovx(s)"})
for table in div:
    tables = table.find("table")
    tbody = tables.find("tbody")
container = tbody.findAll("tr", {"class":"Bgc($extraLightBlue):h"})

Like I said, this worked in the past with the tweaking of the indent of the container variable, but to no avail this time. Did I miss out anything?


Solution

  • The reason is you have code that follows your loop that uses your container variable. At the same time

    div = page_soup.findAll("div", {"class":"Ovx(s)"}
    

    would give you an empty array, so that i won't go into inner part of your loop hence container wouldn't be defined.