[英]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.
一切正常,但我的代码的最后几行 w.text 产生的结果是文本,我需要一个数字。 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.
我尝试了 w.value 或 w.get_attribute('value) 但它不起作用。 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:根据您共享的 HTML:
<span class="good">€25,217.65</span>
The text €25,217.65 is the innerHTML .文本€25,217.65是innerHTML 。
So, you can extract the text €25,217.65 using either:因此,您可以使用以下任一方法提取文本€25,217.65 :
w.get_attribute("innerHTML")
Now to get the value €25,217.65 as a number instead of text you need to:现在要获得价值€25,217.65作为数字而不是文本,您需要:
Remove the €
and ,
character using re.sub()
:使用
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:最后,要将字符串转换为浮点数,您需要将字符串作为参数传递给
float()
,如下所示:
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")
:使用
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:您可以为此使用一些 Python 的内置函数:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.