Home > Software engineering >  ValueError in webscraper: could not convert string to float: '\n €203,88€'
ValueError in webscraper: could not convert string to float: '\n €203,88€'

Time:06-16

Im trying to make a price alert, every time the price of my desired product drops below 190. I get an email. Problem is, there seems to be an issue with my if(converted_price < 190): . It also gives the message that the () are redundant, but if I remove them, the error message doesn't go away. Im following a YouTube tutorial, and doing everything exactly like it is being done. in the video, but still getting an error. This is my full code Output:

from bs4 import BeautifulSoup
import smtplib

URL = 'https://www.amazon.nl/Diadora-DT-FORTY-Tapis-magneetloopband/dp/B00NM8ECHY/ref=sr_1_1?__mk_nl_NL=ÅMÅ' \
      'ŽÕÑ&crid=33LL286IOOEZX&keywords=diadora dt-forty&qid=1655247701&sprefix=diadora dt-forty,aps' \
      ',68&sr=8-1 '

headers = {"User-Agent": 'MYUSERAGENT '
                         'Chrome/102.0.0.0 Safari/537.36 '}


def check_price():

    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html.parser')

    title = soup.find(id="productTitle").get_text()
    price = soup.find(id="corePrice_feature_div").get_text()
    converted_price = float(price[0:10])

    if(converted_price < 190):
        send_mail()

    print(converted_price)
    print(title.strip())

    if(converted_price < 190):
        send_mail()


def send_mail():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login('MYEMAIL', 'MYPASSWORD')

    subject = 'price drop!'
    body = 'Check the link https://www.amazon.nl/Diadora-DT-FORTY-Tapis-magneetloopband/dp/' \
           'B00NM8ECHY/ref=sr_1_1?__mk_nl_NL=ÅMÅŽÕÑ&crid=33LL286IOOEZX&' \
           'keywords=diadora dt-forty&qid=1655247701&sprefix=diadora dt-forty,aps,68&sr=8-1'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail(
        'MYEMAIL',
        'MYEMAIL',
        msg
    )
    print('Email has been sent')

    server.quit()


check_price() ```

**OUPUT**
Traceback (most recent call last):
  File "/Users/julia/PycharmProjects/pythonProject2/pdf.py", line 58, in <module>
    check_price()
  File "/Users/julia/PycharmProjects/pythonProject2/pdf.py", line 21, in check_price
    converted_price = float(price[0:10])
ValueError: could not convert string to float: '\n €203,88€'





CodePudding user response:

You could use another selector for the price element:

Replacing this

price = soup.find(id="corePrice_feature_div").get_text()
converted_price = float(price[0:10])

with this:

converted_price = float(soup.find(class_="a-offscreen").get_text().replace("€", "").replace(",","."))
  • Related