为什么拿不到含硒的hkdrates表?

2024-06-17 12:54:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到含硒的hkdrates表格:

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

chrome_options = Options()
browser = webdriver.Chrome(options=chrome_options,executable_path='/usr/bin/chromedriver')
browser.get("https://www.bochk.com/en/investment/rates/hkdrates.html")

现在该网页包含用selenium的chrome驱动程序打开的hkdrates:
enter image description here

xpath以chrome显示。
enter image description here

browser.find_elements_by_xpath('//*[@id="form-div"]/form/div/table')
[]
browser.find_elements_by_xpath('.//table') 
[]

他们两个什么都拿不到,那怎样才能拿到香港德甲的积分呢?你知道吗


Tags: fromimportdivbrowserformbyseleniumelements
3条回答

要使用Selenium获取hkdrates表,因为所需元素位于<iframe>内,所以您必须:

  • 为所需的frame_to_be_available_and_switch_to_it()诱导WebDriverWait。你知道吗
  • 为所需的visibility_of_all_elements_located()诱导WebDriverWait。你知道吗
  • 您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions()
      options.add_argument("start-maximized")
      driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://www.bochk.com/en/investment/rates/hkdrates.html")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='iframe' and starts-with(@src, '/whk/rates/exchangeRatesHKD/exchangeRatesHKD-input')]")))
      WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "table.form_table.import-data.second-right tbody tr")))
      print(driver.find_element_by_css_selector("table.form_table.import-data.second-right tbody").text)
      
    • 控制台输出:

      Currency Buy Sell
      CNY 90.290000 89.360000
      CNH 90.380000 89.400000
      USD 782.250000 785.250000
      GBP 1,008.800000 1,020.400000
      JPY 721.000000 730.200000
      AUD 536.950000 543.800000
      NZD 500.200000 506.650000
      CAD 591.950000 599.100000
      EUR 868.400000 878.400000
      CHF 790.350000 797.850000
      DKK 116.150000 117.750000
      NOK 85.000000 86.400000
      SEK 80.650000 82.150000
      SGD 575.200000 579.450000
      THB 25.550000 26.550000
      BND 575.200000 579.450000
      ZAR 51.000000 52.950000
      

Here you can find a relevant discussion on Ways to deal with #document under iframe

你的意思是桌子在iframe里面,你需要先换一下。您可以使用以下方法:

.frame_to_be_available_and_switch_to_it

iframe有一个id: iframe

对于您的hkdrates table,您可以使用css selector: .form_table.import-data.second-right

browser.get('https://www.bochk.com/en/investment/rates/hkdrates.html')

WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'iframe')))
hkdrates_tbl = browser.find_element_by_css_selector('.form_table.import-data.second-right')
print(hkdrates_tbl.text)
browser.quit()

以下导入:

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

如果您的表在iframe中,请更改https://www.bochk.com/whk/rates/exchangeRatesHKD/exchangeRatesHKD-input.action?lang=en的url

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

chrome_options = Options()
browser = webdriver.Chrome(options=chrome_options,executable_path='/usr/bin/chromedriver')
browser.get("https://www.bochk.com/whk/rates/exchangeRatesHKD/exchangeRatesHKD-input.action?lang=en")
table=browser.find_elements_by_xpath('//*[@id="form-div"]/form/div/table/tbody/tr')
for x in table:
    print(x.text)

相关问题 更多 >