雅虎财经历史数据下载网址无效

时间:2017-05-18 09:45:05

标签: cookies session-cookies finance yahoo-api yahoo-finance

我使用以下网址从yahoo finance获取历史数据。自2017年5月16日起,网址无效。

http://real-chart.finance.yahoo.com/table.csv?s=AAL&a=04&b=01&c=2017&d=04&e=02&f=2017&g=d&ignore=.csv

好像他们已经更改了网址,新网址是:

https://query1.finance.yahoo.com/v7/finance/download/AAL?period1=1494873000&period2=1494959400&interval=1d&events=history&crumb=l0aEtuOKocj

在上面更改的URL中有一个会话cookie,它是crumb。有没有想过如何以编程方式获取此cookie(在JAVA中)?

7 个答案:

答案 0 :(得分:11)

我最近写了一个简单的python脚本来下载单个股票的历史。
这里有一个如何调用它的例子:
python get_quote_history.py --symbol = IBM --from = 2017-01-01 --to = 2017-05-25 -o IBM.csv
这会将IBM历史价格从2017-01-01下载到2017-05-25,并将它们保存在IBM.csv文件中。

SELECT
    t.Scenario_ID,
    s.Scenario_Name,
    SUM(CASE WHEN t.STATUS = 'Fail' THEN 1 ELSE 0 END) AS sum_fail,
    SUM(CASE WHEN t.STATUS = 'Pass' THEN 1 ELSE 0 END) AS sum_pass
FROM TestCase t
INNER JOIN Scenario s
    ON t.Scenario_ID = s.Scenario_ID
GROUP BY
    t.Scenario_ID,
    s.Scenario_Name

答案 1 :(得分:8)

Andrea Galeazzi的出色答案;添加了分割和分红选项,并为python 3加了扭曲。

同样改变,因此“to:date”包含在返回的结果中,之前的代码返回但不包括“to:date”。与众不同!

请注意,雅虎在价格舍入,列顺序和分割语法方面做了细微更改。

## Downloaded from
## https://stackoverflow.com/questions/44044263/yahoo-finance-historical-data-downloader-url-is-not-working
## Modified for Python 3
## Added --event=history|div|split   default = history
## changed so "to:date" is included in the returned results
## usage: download_quote(symbol, date_from, date_to, events).decode('utf-8')

import re
from urllib.request import urlopen, Request, URLError
import calendar
import datetime
import getopt
import sys
import time

crumble_link = 'https://finance.yahoo.com/quote/{0}/history?p={0}'
crumble_regex = r'CrumbStore":{"crumb":"(.*?)"}'
cookie_regex = r'Set-Cookie: (.*?); '
quote_link = 'https://query1.finance.yahoo.com/v7/finance/download/{}?period1={}&period2={}&interval=1d&events={}&crumb={}'


def get_crumble_and_cookie(symbol):
    link = crumble_link.format(symbol)
    response = urlopen(link)
    match = re.search(cookie_regex, str(response.info()))
    cookie_str = match.group(1)
    text = response.read().decode("utf-8")
    match = re.search(crumble_regex, text)
    crumble_str = match.group(1)
    return crumble_str , cookie_str


def download_quote(symbol, date_from, date_to,events):
    time_stamp_from = calendar.timegm(datetime.datetime.strptime(date_from, "%Y-%m-%d").timetuple())
    next_day = datetime.datetime.strptime(date_to, "%Y-%m-%d") + datetime.timedelta(days=1)
    time_stamp_to = calendar.timegm(next_day.timetuple())

    attempts = 0
    while attempts < 5:
        crumble_str, cookie_str = get_crumble_and_cookie(symbol)
        link = quote_link.format(symbol, time_stamp_from, time_stamp_to, events,crumble_str)
        #print link
        r = Request(link, headers={'Cookie': cookie_str})

        try:
            response = urlopen(r)
            text = response.read()
            print ("{} downloaded".format(symbol))
            return text
        except URLError:
            print ("{} failed at attempt # {}".format(symbol, attempts))
            attempts += 1
            time.sleep(2*attempts)
    return b''

if __name__ == '__main__':
    print (get_crumble_and_cookie('KO'))
    from_arg = "from"
    to_arg = "to"
    symbol_arg = "symbol"
    event_arg = "event"
    output_arg = "o"
    opt_list = (from_arg+"=", to_arg+"=", symbol_arg+"=", event_arg+"=")
    try:
        options, args = getopt.getopt(sys.argv[1:],output_arg+":",opt_list)
    except getopt.GetoptError as err:
        print (err)

    symbol_val = ""
    from_val = ""
    to_val = ""
    output_val = ""
    event_val = "history"
    for opt, value in options:
        if opt[2:] == from_arg:
            from_val = value
        elif opt[2:] == to_arg:
            to_val = value
        elif opt[2:] == symbol_arg:
            symbol_val = value
        elif opt[2:] == event_arg:
            event_val = value
        elif opt[1:] == output_arg:
            output_val = value

    print ("downloading {}".format(symbol_val))
    text = download_quote(symbol_val, from_val, to_val,event_val)
    if text:
        with open(output_val, 'wb') as f:
            f.write(text)
        print ("{} written to {}".format(symbol_val, output_val))

答案 2 :(得分:5)

让它工作,现在我只需要解析csv。我以为自己在使用语法时遇到了麻烦。

Dim crumb As String:    crumb = "xxxx"
Dim cookie As String:   cookie = "yyyy"

Dim urlStock As String: urlStock = "https://query1.finance.yahoo.com/v7/finance/download/SIRI?" & _
    "period1=1274158800&" & _
    "period2=1495059477&" & _
    "interval=1d&events=history&crumb=" & crumb

Dim http As MSXML2.XMLHTTP:   Set http = New MSXML2.ServerXMLHTTP
http.Open "GET", urlStock, False
http.setRequestHeader "Cookie", cookie
http.send

答案 3 :(得分:2)

您可以在Chrome中手动保存crumb / cookie对,也可以使用this之类的内容来生成它。然后,只需在java中设置cookie标头并在URL中传递相应的crumb

答案 4 :(得分:2)

我写了一个轻量级脚本,它将这个线程中的很多建议结合在一起来解决这个问题。 https://github.com/AndrewRPorter/yahoo-historical

但是,还有更好的解决方案,例如https://github.com/ranaroussi/fix-yahoo-finance

希望这些资源有所帮助!

答案 5 :(得分:0)

我在Excel / VBA中针对此问题开发了以下解决方案。关键的挑战是创建Crumb / Cookie对。创建后,您可以重新使用它来获取历史价格的雅虎电话。

请在此处查看Crumb / Cookie的密钥代码

Sub GetYahooRequest(strCrumb As String, strCookie As String)
'This routine will use a sample request to Yahoo to obtain a valid Cookie and Crumb

Dim strUrl                      As String: strUrl = "https://finance.yahoo.com/lookup?s=%7B0%7D"  
Dim objRequest                  As WinHttp.WinHttpRequest

Set objRequest = New WinHttp.WinHttpRequest

    With objRequest
        .Open "GET", strUrl, True
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send
        .waitForResponse
        strCrumb = strExtractCrumb(.responseText)
        strCookie = Split(.getResponseHeader("Set-Cookie"), ";")(0)
    End With

End Sub

请参阅我网站上的以下Yahoo Historical Price Extract以获取演示如何提取Yahoo历史价格的示例Excel工作簿

答案 6 :(得分:0)

很棒的答案Andrea,我已添加到您的代码中以允许下载多个股票。 (python 2.7)

file1:down.py

try {
    Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_emailid@gmail.com"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
    intent.putExtra(Intent.EXTRA_TEXT, "your_text");
    startActivity(intent);
} catch(Exception e) {
    Toast.makeText(Share.this, "Sorry...You don't have any mail app", Toast.LENGTH_SHORT).show();
    e.printStackTrace(); 
}

file2:ticker.csv AAPL MSFT

file3:get_quote_history.py