簡體   English   中英

當我嘗試使用 selenium xpath 抓取 Web 表格文本值時,它給出了空文本值

[英]When i try to scrape web table text values using selenium xpath it gives empty text values

我正在嘗試從以下 URL 年度和季度表中獲取文本值。 但它給出了空值。 這里可能有什么問題。 誰能給我一些幫助? 這些是我試圖提取的值,

在此處輸入圖片說明

這是代碼:在此,我正在嘗試獲取 2018 文本。 我需要框中的所有文本。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser
import os

url = f'https://www.morningstar.ca/ca/report/stocks/financials.aspx?t=0P000000GY&lang=en-CA'

codePath = os.getcwd() + r"\chromedriver_win32\chromedriver"
PATH = os.path.join(codePath)
service = Service(PATH)

service.start()
option = webdriver.ChromeOptions()
# option.add_argument('headless')

driver = webdriver.Remote(service.service_url, options=option)

driver.get(url);
time.sleep(2) 

Y1 = driver.find_elements_by_xpath('/html/body/div[3]/div[1]/div/div[1]/div[1]/div[4]/sal-components/section/div/div/div[2]/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div[2]/div[2]/div/div[2]/div[2]/div/div/table[2]/tbody/tr[1]/th[2]/span')[0].text
print(Y1)

driver.close()

我不太確定您要做什么,但要獲取表格文本值,只需執行以下操作:

假設您的代碼片段工作正常並且問題僅在於 Y1,您可以執行以下操作:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser
import os

url = f'https://www.morningstar.ca/ca/report/stocks/financials.aspx?t=0P000000GY&lang=en-CA'

codePath = os.getcwd() + r"\chromedriver_win32\chromedriver"
PATH = os.path.join(codePath)
service = Service(PATH)

service.start()
option = webdriver.ChromeOptions()
# option.add_argument('headless')

driver = webdriver.Remote(service.service_url, options=option)

driver.get(url);
time.sleep(2) 

Y1 = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//table[@class='report-table ng-isolate-scope']")))

print(Y1.text)

driver.close()

是否有必要使用 WebDriverWait? 答案是否定的,我是用 WebDriverWait 做的,因為你導入了它並且沒有被使用。

上面的代碼打印表格文本值:

Fiscal 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 TTM 5-Yr Index
103.40
Return on Assets % 27.07 28.54 19.34 18.01 20.45 14.93 13.87 16.07 15.69 17.33 23.21 16.19 7.11
Return on Equity % 41.67 42.84 30.64 33.61 46.25 36.90 36.87 49.36 55.92 73.69 103.40 51.97 22.62
Return on Invested Capital % 41.04 42.01 26.08 26.20 31.32 21.95 19.86 24.41 25.75 30.11 40.07 24.98 12.64

您使用了錯誤的定位器。
此外,您必須使用預期條件等待元素加載。 我看到這里有時需要很長時間。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser
import os

url = f'https://www.morningstar.ca/ca/report/stocks/financials.aspx?t=0P000000GY&lang=en-CA'

codePath = os.getcwd() + r"\chromedriver_win32\chromedriver"
PATH = os.path.join(codePath)
service = Service(PATH)

service.start()
option = webdriver.ChromeOptions()
# option.add_argument('headless')

driver = webdriver.Remote(service.service_url, options=option)

driver.get(url);

Y1 = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//div[@class='sal-components-scrollable ng-isolate-scope'])[1]")))

print(Y1.text)

driver.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM