如何使用Selenium和Python从网站上获取价格作为数字 [英] How to get the price as a number from a website using Selenium and Python

查看:97
本文介绍了如何使用Selenium和Python从网站上获取价格作为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个机器人,它将使我的工作自动化并从特定网站复制特定值.一切正常,但是代码的最后几行说w.text会产生一个结果,即文本,我需要一个数字.经过检查后,我需要的值的每个元素如下所示:

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>

如何获取数字而不是文本的值?我尝试了w.value或w.get_attribute('value),但它不起作用.这是我的程序(不包括库和文件的下载)

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

推荐答案

您可以为此使用Python的一些内置函数:

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

  1. str.strip()删除所有前导或尾随€"字符,然后
  2. str.replace()删除,'(将其替换为空字符串")
  1. str.strip() to remove any leading or trailing '€' character, then
  2. str.replace() to remove ',' (replace it with an empty string '')

特别是:

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

这篇关于如何使用Selenium和Python从网站上获取价格作为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆