for x in results:
sponsor = x.findAll(class_="s-sponsored-label-info-icon")
if sponsor == []:
for each in results:
price_data = {
'Item Name': each.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 = price_data["Item Price"].replace("s$", "")
discountpriceclean = price_data["Discounted Price"].replace("s$", "")
ogpriceclean = float(ogpriceclean)
discountpriceclean = float(discountpriceclean)
# 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)
return soup
I'm having issues running this program, which raises this error:
This error which states i'm returning a none after debugging
If I run it without the debugger the following error will arise
ValueError: could not broadcast input array from shape (0,) into shape (1,)
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)