如何解决手动单击链接时,Python和Selenium会弹出一个对话框,而不是自动下载的对话框
我正在使用Python访问SEC网站以下载10-K电子表格。我创建了代码,请求用户输入股票行情代码,成功打开Firefox,访问https://www.sec.gov/edgar/searchedgar/companysearch.html的Edgar搜索页面,并输入正确的股票代码。问题是自动下载电子表格并保存。
现在,我可以手动单击“查看Excel电子表格”,然后自动下载电子表格。但是,当我运行Python代码时,会从Firefox看到一个对话框。我将Firefox设置为自动下载,我尝试使用'find_element_by_xpath','find_element_by_css_selector',但两者都无法简单地下载文件。这两种方法仅调用同一个对话框。我尝试了“ find_element_by_link_text”,但收到有关无法找到“查看Excel电子表格”的错误消息。我的示例股票代号是Caterpillar的CAT(纽约证券交易所:CAT)。我的代码如下:
import selenium.webdriver.support.ui as ui
from pathlib import Path
import selenium.webdriver as webdriver
import time
ticker = input("please provide a ticker symbol: ")
# can do this other ways,but will create a function to do this
def get_edgar_results(ticker):
url = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=" + str(ticker) + "&type=10-k&dateb=20200501&count=20"
# define variable that opens Firefox via my executable path for geckodriver
driver = webdriver.Firefox(executable_path=r"C:\Program Files\JetBrains\geckodriver.exe")
# timers to wait for the webpage to open and display the page itself
wait = ui.WebDriverWait(driver,40)
driver.set_page_load_timeout(40)
driver.get(url)
# timers to have page wait for the page to load.
# seemed that the total amount of time was necessary; not sure about these additional lines
driver.set_page_load_timeout(50)
wait = ui.WebDriverWait(driver,50)
time.sleep(30)
# actual code to search the resulting page for the button to click and access the excel document for download
annual_links = driver.find_element_by_xpath('//*[@id="interactiveDataBtn"]')
annual_links.click()
# need to download the excel spreadsheet itself in the "financial report"
driver.set_page_load_timeout(50)
wait = ui.WebDriverWait(driver,50)
excel_sheet = driver.find_element_by_xpath('/html/body/div[5]/table/tbody/tr[1]/td/a[2]')
excel_sheet.click()
# i'm setting the resulting dialog box to open and download automatically from now on. if i want to change it back
# i'll need to use this page: https://support.mozilla.org/en-US/kb/change-firefox-behavior-when-open-file
# Testing showed that dialog box "open as" probably suits my needs better than 'save'.
driver.close()
driver.quit()
get_edgar_results(ticker)
任何帮助或建议,我们将不胜感激。谢谢!
解决方法
这并不是基于您的实际代码或Selenium的工作方式的建议,而是在尝试从网络上收集信息时的更一般性建议。
如果有机会,通过其API访问网站比通过Selenium尝试相同的任务对编程更友好。当您使用Selenium进行网络爬网时,通常情况下,网站的行为与通过普通浏览器访问时的行为不同。这可能有多种原因,其中最重要的原因可能是网站有意阻止了Selenium之类的自动浏览器访问它们。
在这种情况下,EDGAR SEC provides an HTTPS access service可以使您获得所需的信息。
无需深入研究这些数据,使用诸如requests
之类的http请求库来请求此信息并以这种方式进行保存应该不是很困难。
import requests
result = requests.get("https://www.sec.gov/Archives/edgar/data/18230/000001823020000214/Financial_Report.xlsx")
with open("file.xlsx","wb") as excelFile:
excelFile.write(result.content)
唯一的困难在于获得股票行情指示器的CIK来构建上述URL,但是使用相同的API信息应该不会太困难。
EDGAR网站通过其URL相当透明地向您公开其数据。您可以绕过所有Selenium怪异之处,而只需构建URL,然后直接请求信息,而无需加载所有JavaScript等。
编辑:您也可以以更具编程性的方式浏览此信息。上面的链接提到edgar / full-index中的每个目录还提供了一个JSON文件,该文件易于计算机读取。因此,您可以请求https://www.sec.gov/Archives/edgar/full-index/index.json
,解析出您想要的年份,请求该年,解析出您想要的季度,请求该季度,然后解析出您想要的公司,并要求该公司的信息,等等。>
例如,要获取Caterpillar的CIK编号,您将从company.gz
中获取并解析您的https://www.sec.gov/Archives/edgar/full-index/2020/QTR4.json
文件,将其解析为数据帧,然后在其上找到带有CATERPILLAR INC
的行,从关联的.txt文件中找到CIK和登录号,然后找到正确的URL以下载其Excel文件。有点circuit回,但是如果您可以找到一种方法来跳至CIK号码,则可以减少所需的请求数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。