我想从链接中找到TTM EPS的值https://www.moneycontrol.com/india/stockpricequote/computers-software/infosys/IT
我写了以下代码:
import os
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--start-maximized')
options.page_load_strategy = 'eager'
driver = webdriver.Chrome(options=options)
company = "Infosys"
wait = WebDriverWait(driver, 20)
driver.get("https://www.moneycontrol.com")
inputElement = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
inputElement.send_keys(company, Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()
driver.implicitly_wait(30)
driver.find_element_by_class_name("oview_table")
现在,存在多个具有相同名称的类";oview_table";及其表格。如何获取TTM EPS的价值?
尝试以下xpath
//td[contains(text(),'TTM EPS')]/../td[contains(@class,'nseceps')]
这是您要查找的元素
现在您可以从中提取文本值。
为了获得任何其他资产的值,只需将其作为参数传递到此字符串
这个xpath也应该工作。输入您要查找的文本。
//div[@class='oview_table']//td[contains(normalize-space(.), 'TTM EPS')]