简体   繁体   English

使用 python 从 web 页面从“值”中获取数字

[英]get number from “value” from a web page using python

So I used python to open a web page in which python would enter a number in a box (amount) like 100 then the website generates a number in another box (quantity) or vice versa, the price changes every day.所以我用 python 打开了一个 web 页面,其中 python 会在一个盒子里输入一个数字(数量),比如 100,然后网站会在另一个盒子里生成一个数字(数量),反之亦然。

the webpage code for the quantity box after it generated the value (quantity) is shown below:数量框生成值(数量)后的网页代码如下所示:

<div class="sc-62mpio-0 sc-1c2873k-4 jclRvn">
<input type="number" id="FormRow-BUY-quantity" name="quantity" step="0.00001" min="0.00001" class="sc-1c2873k-1 fIfOtX" value="2.37812">
<span class="sc-1c2873k-7 dmFXTy">LTC</span>
</div>

I want to save the value generated in the code above (2.37812).我想保存上面代码中生成的值(2.37812)。

the XPath for the quantity box mentioned above is:@id="FormRow-BUY-quantity上述数量框的 XPath 为:@id="FormRow-BUY-quantity

I used the following code to send the amount of 100 into the amount box我使用以下代码将 100 的金额发送到金额框

  driver.find_element_by_xpath('(//input[@name="total"])[1]').send_keys("100", Keys.ENTER)

I would like to store the value in a string to do some math with it.我想将值存储在一个字符串中以对其进行一些数学运算。

Ideally you should be using get_attribute which can read html attribute value for any tag理想情况下,您应该使用get_attribute可以读取任何标签的 html 属性值

text1=driver.find_element_by_xpath('//*[@id="FormRow-BUY-quantity"]').get_attribute("value")
print(text1)

If you want to convert read attribute from String to Float then you can use it like如果要将读取属性从 String 转换为 Float,则可以像这样使用它

text1=driver.find_element_by_xpath('//*[@id="FormRow-BUY-quantity"]').get_attribute("value")
print(float(text1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM