簡體   English   中英

將溫度轉換為 int

[英]Converting temperature to int

我正在使用 selenium 在網站上查找以“39 c”這種格式顯示溫度的 Web 元素。 反正有沒有把它轉換成 int 以便我可以使用像 <、=>、< 等運算符?


temperature = driver.find_element(By.ID, "temperature")
temperature = int(float(temperature.text))



if temperature < 19:
    driver.find_element_by_link_text('Buy sunscreens').click()
elif temperature > 34:
    driver.find_element_by_link_text('Buy moisturizers').click();

ValueError: 無法將字符串轉換為浮點數:'9 ℃'

你可以使用str.split

temperature = int(temperature.text.split()[0])

或使用re

temperature = int(re.search('\d+', temperature.text)[0])

您可以根據空格拆分字符串,然后可以對其調用int

temperature = driver.find_element(By.ID, "temperature")
temperature_int = int(temperature.text.split(' ')[0])
print(type(temperature_int))
print(temperature_int)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM