I'm using beautiful soup to extract information from a website and to get the price of an item. I used the code below to create a variable names prices to store the information. Then I created a loop to iterate through all of the items and now I'm trying to compare it to another variable named price_thres to determine if its less than or equal to the amount. Running this code prints a few of the correct values but it also prints
price_in_dec = Decimal(i.text)
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
prices = soup.find_all("span", itemprop="price")
for i in prices:
price_in_dec = Decimal(i.text)
if price_in_dec <= price_thres:
print(price_in_dec)
You seem to have commas (,
) in some of your prices.
Try this:
import locale
# Set to US locale
locale.setlocale(locale.LC_ALL, 'USA')
prices = soup.find_all("span", itemprop="price")
for i in prices:
price_in_dec = Decimal(locale.atof(i.text))
if price_in_dec <= price_thres:
print(price_in_dec)
By using the correct locale, you can parse the prices according to the way you write them in the US.
Another option would be to simply remove commas using i.text.replace(",", "")
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments