我试图一贯地从一个链接下载历史股票数据,该链接出现在您将光标悬停在页面上之后。目前,我有以下代码,这些代码似乎没有找到css_selector,也没有下载.csv文件。
#!/usr/bin/env python3.6
## Import Libraries
import os, sys
import time
from selenium import webdriver
import selenium.webdriver.firefox.options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
## Declare Variables
ticker = 'CAT'
period1 = '1262332800'
period2 = '1537945200'
download_path = os.getcwd()
css_selector = "a.Fl\(end\):nth-child(1)"
## Configure Firefox Options
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2) # 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
profile.set_preference("browser.download.dir", download_path)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip/text/csv")
## Firefox driver loads historical data page
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://finance.yahoo.com/quote/{}/history?period1={}&period2={}&interval=1d&filter=history&frequency=1d"
.format(ticker, period1, period2))
## Click on 'Download Data' Link
try:
input_element = driver.find_element_by_css_selector(css_selector).click()
print('Success!')
except:
print('Failed!!!!!')
finally:
driver.quit()
print('Kill Driver!')css_selector,"a.Fl(end):nth-child(1)",可在本节中找到:
<svg class="Va(m)! Mend(5px) Stk($c-fuji-blue-1-b)! Fill($c-fuji-blue-1-b)! Cur(p)" width="15" height="15" viewBox="0 0 48 48" data-icon="download" style="fill: rgb(0, 129, 242); stroke: rgb(0, 129, 242); stroke-width: 0; vertical-align: bottom;"><path d="M43.002 43.002h-38c-1.106 0-2.002-.896-2.002-2v-11c0-1.105.896-2 2.002-2 1.103 0 1.998.895 1.998 2v9h34.002v-9c0-1.105.896-2 2-2s2 .895 2 2v11c0 1.103-.896 2-2 2m-19-8L11.57 23.307c-.75-.748-.75-1.965 0-2.715.75-.75 1.965-.75 2.715 0l7.717 7.716V2h4v26.308l7.717-7.716c.75-.75 1.964-.75 2.714 0s.75 1.967 0 2.715L24.002 35.002z"></path></svg><span>Download Data</span>我的问题是:
使用.find_element_by_link_text()方法将得到TimeoutException:
TimeoutException跟踪(最近一次调用)在() 21 ##转到主页获取历史数据 22 driver.get("https://finance.yahoo.com/quote/{}/history?period1={}&period2={}&interval=1d&filter=history&frequency=1d“ -> 23 .format(代码机,period1,period2) 24 25打印(‘.get()完成!’).get在get(self,url) 331在当前浏览器会话中加载网页。 332“” -> 333 self.execute(Command.GET,{'url':url}) 334 执行中的335 @property ~/virtualenvs/demo/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py (self、driver_command、params) 319响应= self.command_executor.execute(driver_command,params) 320如果答复: ->321个self.error_handler.check_response(答复) 322响应‘值’= self._unwrap_value( 323 response.get('value',None)response.get in check_response(self,response) 240 alert_text =值‘警报’.获取(‘文本’) 提出exception_class(消息、屏幕、堆栈跟踪、alert_text) ->242个引发exception_class(消息、屏幕、堆栈跟踪) 243 244个def _value_or_default(self,obj,key,default):TimeoutException: Message:超时加载页面,在300000 def之后
我对此的解释是,站点没有完成加载,因此不会执行try/ not /finally逻辑。
发布于 2018-10-03 19:20:29
更新
如果需要停止页面加载,请尝试以下解决方案:
from selenium.common.exceptions import TimeoutException
driver.set_page_load_timeout(10)
try:
driver.get("https://finance.yahoo.com/quote/{}/history?period1={}&period2={}&interval=1d&filter=history&frequency=1d"
.format(ticker, period1, period2))
except TimeoutException:
driver.execute_script("window.stop();")
driver.find_element_by_link_text('Download Data').click()如果没有在10秒内加载页面,页面加载将被强制停止。
发布于 2018-10-03 19:37:24
你能试试下面的选项吗
1. download = driver.find_element_by_xpath(".//*[@id='Col1-1-HistoricalDataTable-Proxy']/section/div[1]/div[2]/span[2]/a")
download.click()
2. download = driver.find_element_by_link_text('Download Data')
download.click()
3. download = driver.find_element_by_partial_link_text('Download')
download.click()https://stackoverflow.com/questions/52634137
复制相似问题