如何从不断变化并连接到数据库的网站获取值?
How to get values from website which is always changing and connected to database?
我正在根据需要使用 python
、selenium
和其他软件包。我正在创建 GUI(图形用户界面),它将显示通过门的总人数。
我们已经有了直接向特定网站报告的硬件,这些网站是用 php
编写的,数据库是 sql
和 mariadb.
<span class="info-box-number f28 fc-666" id="tot_count">11</span>
这是代码示例,我想从中捕获“11”。我尝试使用 'id'、'class' 和 'xpath' 来使用 .text
捕获此问题,但到目前为止我找不到解决方案。我提出了一些其他问题,但对我没有帮助。
- 所以,想法是创建每秒更新一次并显示通过人数的 GUI 界面。我确实必须用它获得一些其他值,这些值也以类似的格式,具有不同 class、id 和 xpath 的相同数据库。
注意: 所有 class 和 id 都是唯一的,我想捕获。
如果有人有更好的想法,我愿意接受建议,
您的建议将不胜感激。
要打印 text 11
您可以使用以下任一方法 :
使用class_name
和get_attribute("textContent")
:
print(driver.find_element_by_class_name("info-box-number").get_attribute("textContent"))
使用css_selector
和get_attribute("innerHTML")
:
print(driver.find_element_by_css_selector("span.info-box-number#tot_count").get_attribute("innerHTML"))
使用 xpath
和 text 属性:
print(driver.find_element_by_xpath("//span[contains(., 'info-box-number') and @id='tot_count']").text)
理想情况下,您需要为 visibility_of_element_located()
引入 ,您可以使用以下任一项 :
使用CLASS_NAME
和get_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)
使用XPATH
和get_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
我正在根据需要使用 python
、selenium
和其他软件包。我正在创建 GUI(图形用户界面),它将显示通过门的总人数。
我们已经有了直接向特定网站报告的硬件,这些网站是用 php
编写的,数据库是 sql
和 mariadb.
<span class="info-box-number f28 fc-666" id="tot_count">11</span>
这是代码示例,我想从中捕获“11”。我尝试使用 'id'、'class' 和 'xpath' 来使用 .text
捕获此问题,但到目前为止我找不到解决方案。我提出了一些其他问题,但对我没有帮助。
- 所以,想法是创建每秒更新一次并显示通过人数的 GUI 界面。我确实必须用它获得一些其他值,这些值也以类似的格式,具有不同 class、id 和 xpath 的相同数据库。
注意: 所有 class 和 id 都是唯一的,我想捕获。
如果有人有更好的想法,我愿意接受建议, 您的建议将不胜感激。
要打印 text 11
您可以使用以下任一方法
使用
class_name
和get_attribute("textContent")
:print(driver.find_element_by_class_name("info-box-number").get_attribute("textContent"))
使用
css_selector
和get_attribute("innerHTML")
:print(driver.find_element_by_css_selector("span.info-box-number#tot_count").get_attribute("innerHTML"))
使用
xpath
和 text 属性:print(driver.find_element_by_xpath("//span[contains(., 'info-box-number') and @id='tot_count']").text)
理想情况下,您需要为 visibility_of_element_located()
引入
使用
CLASS_NAME
和get_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)
使用
XPATH
和get_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