Home > Back-end >  Python Selenium Webscraping project. Cutting a calculation result to 3 digits
Python Selenium Webscraping project. Cutting a calculation result to 3 digits

Time:07-28

I'm working on a web scraping project with Python Selenium. I need to get a variable price from a website for airplane tickets and then calculate how many times the normal (bottom price) fits into the new (variable) price. I need a result like: 1.5x, 1,6, 2.0x etc.

When I run my code

Standard_price = 0.00042427
new_price = driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[1]/main/section[1]/div/div[2]/div/div/div[1]/div/div[3]/div[2]/div[2]/div[1]/div/span').get_attribute('innerHTML')
emp_str = ""
for m in new_price:
    if m.isdigit():
        emp_str = emp_str   m
print(Standard_price * float(emp_str))

I get the result: 1.17607644

I need the result to be a maximum of 3 digits: 1.17

I think I went through every question on Stackoverflow, and tried all the examples, unfortunately with no result.

Hope someone can help with this problem.

PS: I've used isdigit so I can pull out only numbers out of the XPATH source, otherwise I would get a nbsp with the result.

After that I made it a float in the print command so it would calculate correctly with the Standard_price, considering Standard_price is a float, and the result of the XPATH Source is a string. Any other more practical solution is also welcome.

CodePudding user response:

Did you try:

price_in_right_format = round(Standard_price * float(emp_str), 2)
  • Related