问题
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
回答1:
You could use some of Python's built in functions for that:
- str.strip() to remove any leading or trailing '€' character, then
- 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
回答2:
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 usingre.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")))
来源:https://stackoverflow.com/questions/62406791/how-to-get-the-price-as-a-number-from-a-website-using-selenium-and-python