selenium无法检索/阅读网站的一部分

时间:2016-07-09 00:21:02

标签: python selenium web-crawler

我正在尝试使用自动脚本来下载一些外汇价格历史数据。

提供了这些数据

https://www.dukascopy.com/swiss/english/marketwatch/historical/

但是,如果我只是使用Selenium来检索网站的一部分,无论我等多久,我都无法在网站上找到这个元素:

<iframe src="https://freeserv.dukascopy.com/2.0/?path=historical_data_feed/index&amp;header=false&amp;availableInstruments=l%3A&amp;width=100%25&amp;height=600&amp;adv=popup" border="0" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" width="100%" height="600"></iframe>

显然,硒能够读取的唯一内容是:

之前的两个元素
<script type="text/javascript">DukascopyApplet = {"type":"historical_data_feed","params":{"header":false,"availableInstruments":"l:","width":"100%","height":"600","adv":"popup"}};</script>

<script type="text/javascript" src="https://freeserv.dukascopy.com/2.0/core.js"></script>

任何人都知道如何解决这个问题?非常感谢。

1 个答案:

答案 0 :(得分:0)

框架的

Wait for the presence然后切换到它。然后,您可以查找内部元素:

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


driver = webdriver.Firefox()
driver.get("https://www.dukascopy.com/swiss/english/marketwatch/historical/")

# wait for the frame to load and switch
wait = WebDriverWait(driver, 10)
iframe = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".mainContentBody iframe")))
driver.switch_to.frame(iframe)

# print out the bold radio button style texts
for item in driver.find_elements_by_css_selector("ul > li[data-group][data-instrument] strong"):
    print(item.text)

driver.close()

打印:

ADS.DE
ALV.DE
AUD/CAD
AUD/CHF
AUD/JPY
...
VOW3.DE
XAG/USD
XAU/USD
ZAR/JPY