【问题标题】:Yahoo Finance Download Data雅虎财经下载数据
【发布时间】:2019-09-12 02:28:11
【问题描述】:

我正在尝试抓取 Finance.yahoo.com 并下载数据文件。具体来说,这个网址:https://finance.yahoo.com/quote/AAPL/history?p=AAPL

我想在这里完成两个目标:

  1. 我想将数据时间段参数设置为“Max”,我认为我需要使用 Selenium 和
  2. 想要下载并保存嵌入在检查“下载数据”时出现的 href 中的数据文件。

到目前为止,我无法访问单击“Max”所需的下拉菜单,也无法找到下载文件所需的 href。

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

options = webdriver.ChromeOptions()
options.add_argument('--log-level=3')

stock = input()
base_url = 'https://finance.yahoo.com/quote/{}/history?p= 
{}'.format(stock,stock)
driver = webdriver.Chrome()
driver.get(base_url)
driver.maximize_window()
driver.implicitly_wait(4)
driver.find_element_by_class_name("Fl(end) Mt(3px) Cur(p)").click()
time.sleep(4)
driver.quit()

【问题讨论】:

  • 请包含您当前的代码。有很多 Stackoverflow 示例可以帮助您与 Yahoo Finance 合作。如果我们看不到您的代码,我们将无法帮助您修复它。
  • 当然!我已经添加了我当前的代码 - 我正在尝试找到“下载数据”href,以便我可以下载数据并在熊猫中执行一些基本操作。

标签: python web-scraping finance


【解决方案1】:

以下显示了您可以使用的选择器。我没有添加任何等待条件作为唯一需要的条件,在我的测试运行中,我找不到;按下应用按钮后等待所有新数据出现。相反,我使用了一个硬编码的 time.sleep(5),如果可能的话,应该用一个更好的基于条件的等待来代替。

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
import time

d = webdriver.Chrome()
d.get('https://finance.yahoo.com/quote/AAPL/history?p=AAPL')
try:
    d.find_element_by_css_selector('[name=agree]').click() #oauth
except:
    pass

d.find_element_by_css_selector('[data-icon=CoreArrowDown]').click() #dropdown
d.find_element_by_css_selector('[data-value=MAX]').click() #max
d.find_element_by_css_selector('button.Fl\(start\)').click() # done
d.find_element_by_css_selector('button.Fl\(end\) span').click() #apply
time.sleep(5)
d.find_element_by_css_selector('[download]').click() #download

【讨论】:

  • 感谢您向我展示如何执行此操作。我能够检查并找到每个下拉列表、最大、完成、应用和下载的相对 xpath。我专门练习如何使用 selenium 在 HTML 树中定位它们。感谢您的帮助。
【解决方案2】:

您可以立即消除 #1 —— 只需直接查看页面,按要求传递参数。

基本 URI 是:https://finance.yahoo.com/quote/AAPL/history

可用参数有:period1period2intervalfilterfrequency

非常简单,只需将 now 抓取为纪元时间戳,并将其用作 period2 参数,其中 period1 可以简单地作为开始纪元 0intervalfrequency 可以是任何你想要的;每天1d,每周1wk 或每月1mo。最后,filter 应该是history

完整的 URI:

https://finance.yahoo.com/quote/AAPL/history?period1=0&period2=1555905600&interval=1d&filter=history&frequency=1d

从那里,使用 Selenium 定位并单击 Download Data 链接。

更新:

正如@QHarr 所说,Stack Overflow 上存在许多问题,详细说明了如何与雅虎财务合作。我还建议您试一试searching

【讨论】:

  • 感谢您对第一个问题的帮助。作为对 Selenium 的回应,我在发布问题之前使用了它,并将其包含在我的编辑中 - 我将在以后的任何问题中包含它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
相关资源
最近更新 更多