繁体   English   中英

如何使用 python、selenium 和 chromedriver 从网站中提取此值

[英]how can i extract this value from a website, with python, selenium and chromedriver

<div class="flexible row ng-scope">
    <!-- ngRepeat: graph in graphs track by $index --><figure class="figure-gauge flexible column ng-scope" data-ng-repeat="graph in graphs track by $index">
        <figcaption class="rigid">
            <div class="data-value">
                <b class="ng-binding">
                    334
                </b>
            </div>

我需要提取出现的值 (334),我从 Chrome Web Inspector 中获取。 我正在使用 python 和 selenium,我尝试了很多代码,但没有一个对我有用,我感谢任何帮助。

我试试这个:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

DRIVER_PATH = r'C:\chromedriver.exe'
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")

driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
driver.get("https://iot.app.initialstate.com/embed/#/tiles/bkt_kb448kawjvmhe")
data = driver.find_element_by_class_name('ng-binding')
print
driver.quit()

错误:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“.ng-binding”}(会话信息:headless chrome = 83.0 .4103.61)

Selenium 3.141.0 Python 3.8

您可以使用xpath提取它

val = driver.find_element_by_xpath('/html/body/div[1]/article/div[2]/div/div/div[1]/article/div/figure/figcaption/div/b').text
print(val)

您需要提供一些sleep()来加载图表上的值,然后使用下面的xpath来获取值。

driver.get("https://iot.app.initialstate.com/embed/#/tiles/bkt_kb448kawjvmhe")
time.sleep(3)
print(driver.find_element_by_xpath("(//div[@class='data-value']/b)[last()]").text.strip())

暂无
暂无

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

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