一直在使用Selenium Webdriver,我想从网页中提取某些信息。下面是webdriver的源代码
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "/usr/local/bin/chromedriver")
driver = webdriver.Chrome ("/usr/local/bin/chromedriver")
browser = webdriver.Chrome(executable_path = DRIVER_BIN)
browser.get('https://www.abuseipdb.com/check/95.47.155.87')
我想从该特定网站提取91%的值并将其保存到变量中。将需要一个能够提取%值的解决方案,即使它不是91%。以下是html代码。
<p>This IP was reported <b>222</b> times. Confidence of Abuse is <b>91%</b>: <a href="/faq.html#confidence" style="float: right; font-weight: bold;" class=text-muted>?</a></p>
答案 0 :(得分:0)
这将使用 Xpath 使用带有<b>
的{{1}}标签的 元素的第一个文本:
%
答案 1 :(得分:0)
您可以使用@PixelEinstein
中提到的xpath选择器,也可以使用以下css选择器获取文本值
text_of_element = browser.find_element_by_css_selector('div.well > p:nth-child(2) >b:nth-child(2)').text
print(text_of_element)
这将打印91%
标签的值<p>
。
答案 2 :(得分:0)
要提取文本,例如 91%,您可以使用以下解决方案:
driver.find_element_by_xpath("//div[@class='well']/h3//following::p[1]//following::b[2]").get_attribute("innerHTML")