Home > database >  Local variable undefined
Local variable undefined

Time:10-08

for each in results:
    # To get the name of the item
    name = each.h2.a.text.strip()

    # To get the rating of the item
    hasrating = each.i
    if hasrating is not None:
        itemrating = each.i.text
    else:
        itemrating = "No rating"

    # To get the types of prices realprice = non discount \ disprice = discounted price
    hasdisprice = each.find(class_="a-price")
    hasrealprice = each.find(class_="a-price a-text-price")

    if hasrealprice is not None and hasdisprice is not None:
        ogprice = hasrealprice.find(class_="a-offscreen").text.strip()
        discountprice = hasdisprice.find(class_="a-offscreen").text.strip()
        ogpriceclean = float(ogprice.replace("s$",""))
        discountpriceclean = float(discountprice.replace("s$",""))

        # Finding discounted percentage
        discountpercentage = findpercentage(ogpriceclean,discountpriceclean)

    else:

        if hasrealprice is not None:

            itemprice = hasrealprice.find(class_="a-offscreen").text.strip()

        else:
            itemprice = "Not available"

        if hasdisprice is not None:

            disitemprice = hasdisprice.find(class_="a-offscreen").text.strip()

        else:
            disitemprice = "Not Available"



    # To get the url of each item

    urlitem = each.find(class_="a-link-normal a-text-normal").get('href')

    # Data framing all of the items that was scraped

    df = df.append(pd.DataFrame({'Item Name': name, 'Item Price': itemprice, 'Discounted Price': disitemprice,'Percentage of discount': discountpercentage, 'Item rating': itemrating, 'Url': urlitem}, index=[0]), ignore_index=True)

I'm having issues running this program, which raises this error:

UnboundLocalError: local variable 'discountpercentage' referenced before assignment

I do not know what is causing this error, or how to clean up this code.

CodePudding user response:

This happens when you try to use a variable before it has been assigned in the local context. Try to declare it before using it.

CodePudding user response:

While you don't cite a line number that the error arises, it is likely the last line:

df = df.append(pd.DataFrame({'Item Name': name, 'Item Price': itemprice, 'Discounted Price': disitemprice,'Percentage of discount': discountpercentage, 'Item rating': itemrating, 'Url': urlitem}, index=[0]), ignore_index=True)`

Here you assign discountpercentage, but this may not have been defined, in particular because of your if/else cases:

if hasrealprice is not None and hasdisprice is not None:
    ...
    # discountpercentage gets assigned here
else:
    if hasrealprices is not None:
        ...
        # No assignment here
    else:
        ...
        # or here
    if hasdisprice is not None:
        ...
        # or here
    else:
        ...
        # or here

So, part of what is at issue here is that your structure is sort of asking for a lot of copy-paste wiring. What I would recommend is to start with the object that you need to consume in that last line, specifically a dictionary, and then fill it in:

for result in results:
    price_data = {
      'Item Name': result.h2.a.text.strip(), 
      'Item Price': "Not Available", 
      'Discounted Price': "Not Available",
      'Percentage of discount': 0.0, 
      'Item rating': each.i if each.i is not None else "No rating", 
      'Url': each.find(class_="a-link-normal a-text-normal").get('href')
    }

    # To get the types of prices realprice = non discount \ disprice = discounted price
    hasdisprice = each.find(class_="a-price")
    hasrealprice = each.find(class_="a-price a-text-price")

    if hasrealprice is not None and hasdisprice is not None:
        price_data["Item Price"] = hasrealprice.find(class_="a-offscreen").text.strip()
        price_data["Discounted Price"] = hasdisprice.find(class_="a-offscreen").text.strip()
        ogpriceclean = float(price_data["Item Price"].replace("s$",""))
        discountpriceclean = float(price_data["Discounted Price"].replace("s$",""))

        # Finding discounted percentage
        price_data["Percentage of discount"] = findpercentage(ogpriceclean, discountpriceclean)
    elif hasdisprice is None:  # We know hasrealprice is not None
        price_data["Item Price"] = hasrealprice.find(class_="a-offscreen").text.strip()
    elif hasrealprice is None:  # We know hasdisprice is not None
        price_data["Discounted Price"] = hasdisprice.find(class_="a-offscreen").text.strip()
    else:  # We know both are None
        pass  # We could assign something here if needed in this case

    # print(f"Price data={price_data}")  # Uncomment to make sure it looks right

    df = df.append(pd.DataFrame(price_data, index=[0]), ignore_index=True)
  • Related