简体   繁体   中英

How to get the price as a number from a website using Selenium and Python

I am creating a bot that would automate my work and copy particular values from a particular website. Everything works fine but the last lines of my code that says w.text produces an outcome which is text and I need a number. Each element that I need the value of looks like this after inspection:

<span class="good">€25,217.65</span>

How do I get the value as a number instead of as a text? I tried w.value or w.get_attribute('value) but it doesn't work. Here is my program (excluding downloads of libraries and files)

driver = webdriver.Chrome(driver_path)   
driver.get('https://seabass-admin.igp.cloud/')   
# waiting for login table to load
try:
    element = WebDriverWait(driver,10).until(
    ec.presence_of_element_located((By.XPATH,'//*[@id="email"]'))
    )
except:
    driver.quit()

#entering sensitive info
driver.find_element_by_id("email").send_keys(pwx.em)                                  # login details
driver.find_element_by_id("password").send_keys(pwx.pw)                               # password 
details
driver.find_element_by_xpath('//*[@id="appContainer"]/div/form/button').click()       # click sign in

# waiting for page to load
try:
    element = WebDriverWait(driver,10).until(
    ec.presence_of_element_located((By.XPATH,'//* 
[@id="testing"]/section/section[4]/div/table/tbody/tr[2]/td[3]/span'))
    )
except:
    driver.quit()

# getting info from the page
w = driver.find_element_by_xpath('//* 
[@id="testing"]/section/section[4]/div/table/tbody/tr[2]/td[3]/span')
cell = outcome['import']
cell[withdrawal_cell].value = w.text

As per the HTML you have shared:

<span class="good">€25,217.65</span>

The text €25,217.65 is the innerHTML .

So, you can extract the text €25,217.65 using either:

  • w.get_attribute("innerHTML")
  • text attribute.

Now to get the value €25,217.65 as a number instead of text you need to:

  • Remove the and , character using re.sub() :

     import re string = "€25,217.65" my_string = re.sub('[€,]', '', string)
  • Finally, to convert the string to float you need to pass the string as an argument to the float() as follows:

     my_number = float(my_string)

So the entire operation in a single line:

import re

string = "€25,217.65"       
print(float(re.sub('[€,]', '', string)))

Effectively, your line of code can be either of the following:

  • Using text attribute:

     cell[withdrawal_cell].value = float(re.sub('[€,]', '', w.text))
  • Using get_attribute("innerHTML") :

     cell[withdrawal_cell].value = float(re.sub('[€,]', '', w.get_attribute("innerHTML")))

You could use some of Python's built in functions for that:

  1. str.strip() to remove any leading or trailing '€' character, then
  2. str.replace() to remove ',' (replace it with an empty string '')

Specifically:

str_w = w.text  # this is the '€25,217.65' string
digits=str_w.strip('€').replace(',','')     # use the functions above to get number-like string
cell[withdrawal_cell].value = float(digits)   # convert to float number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM