简体   繁体   English

在Python中使用Selenium来选择/复制文本

[英]Using Selenium in Python to select/copy a text

<div class="top-info">
"Vote : 42 - Out : 84 - Rate : 3 - Strategie - "
<a href="/site-Ezy+1.29++Serveur+FUN++Serveur+AnkaLike+-44578">Info</a>
</div>

Hi, I'm trying to select and copy the out which equals 84 in this case number from this code but I don't know what to do? 嗨,我正在尝试从这段代码中选择并复制出这个案例编号为84的out,但我不知道该怎么办?

First of all you would need to get a reference to the html element that contains the needed text. 首先,您需要获取包含所需文本的html元素的引用。 You could use the driver.find_element_by_class_name('top-info') method from the webdriver (assuming the page uses this class only once). 您可以使用webdriver中的driver.find_element_by_class_name('top-info')方法(假设页面仅使用此类一次)。 You can find more HTML elements with selenium here . 您可以在此处找到更多带有selenium的HTML元素。

When you have a reference to that HTML element you can access the inner text by using the following method on your previously found element: 当您引用该HTML元素时,可以通过在先前找到的元素上使用以下方法来访问内部文本:

text = element.get_attribute('innerHTML')

The last thing you need to do is split your string in order to get the number you need. 您需要做的最后一件事是拆分字符串以获得所需的数字。 You could do something like this: 你可以这样做:

text.split(":")[2].split(" ")[0]

This splits the text first at all colons, then it would split the third part at all spaces and finally grabs the first part of the result. 这会首先在所有冒号处拆分文本,然后在所有空格处拆分第三部分,最后抓取结果的第一部分。 This would give you the number 84 in your example. 这将为您提供示例中的数字84。

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

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