如何从不断变化并连接到数据库的网站获取值?

How to get values from website which is always changing and connected to database?

我正在根据需要使用 pythonselenium 和其他软件包。我正在创建 GUI(图形用户界面),它将显示通过门的总人数。 我们已经有了直接向特定网站报告的硬件,这些网站是用 php 编写的,数据库是 sqlmariadb.

<span class="info-box-number f28 fc-666" id="tot_count">11</span>

这是代码示例,我想从中捕获“11”。我尝试使用 'id'、'class' 和 'xpath' 来使用 .text 捕获此问题,但到目前为止我找不到解决方案。我提出了一些其他问题,但对我没有帮助。

注意: 所有 class 和 id 都是唯一的,我想捕获。

如果有人有更好的想法,我愿意接受建议, 您的建议将不胜感激。

要打印 text 11 您可以使用以下任一方法 :

  • 使用class_nameget_attribute("textContent"):

    print(driver.find_element_by_class_name("info-box-number").get_attribute("textContent"))
    
  • 使用css_selectorget_attribute("innerHTML"):

    print(driver.find_element_by_css_selector("span.info-box-number#tot_count").get_attribute("innerHTML"))
    
  • 使用 xpathtext 属性:

    print(driver.find_element_by_xpath("//span[contains(., 'info-box-number') and @id='tot_count']").text)
    

理想情况下,您需要为 visibility_of_element_located() 引入 ,您可以使用以下任一项 :

  • 使用CLASS_NAMEget_attribute("textContent"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "info-box-number"))).get_attribute("textContent"))
    
  • 使用CSS_SELECTOR文本属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.info-box-number#tot_count"))).text)
    
  • 使用XPATHget_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'info-box-number') and @id='tot_count']"))).get_attribute("innerHTML"))
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in


参考资料

Link 到有用的文档:

  • get_attribute()方法Gets the given attribute or property of the element.
  • text属性returnsThe text of the element.
  • Difference between text and innerHTML using Selenium