【问题标题】:Yahoo Finance API [closed]雅虎财经 API [关闭]
【发布时间】:2009-11-19 13:30:13
【问题描述】:

问。雅虎是否提供任何金融 API?如果是,该 API 的链接是什么。

【问题讨论】:

  • 另外,请考虑合并财务数据 API:mergent.com/servius
  • 您可以使用免费 API jarloo.com/yahoo_finance获取最多 200 个符号的 CSV 文件
  • @DaveWebb Google 的 Finance API 已被弃用,将于 2012 年 10 月停止使用:(
  • Google Finance API 现已弃用... :(
  • 不管有什么价值,我都会尝试更新财务 API 的here 列表。截至 2013 年 8 月 26 日,雅虎和谷歌似乎都有可用的 API,尽管我不知道它们的许可。

标签: java yahoo-finance


【解决方案1】:

恕我直言,查找此信息的最佳位置是: http://code.google.com/p/yahoo-finance-managed/

我也曾经使用过“gummy-stuff”,但后来我发现这个页面更有条理,并且充满了易于使用的示例。我现在使用它来获取 CSV 文件中的数据并在我的 C++/Qt 项目中使用这些文件。

【讨论】:

    【解决方案2】:

    这是我在 c# 中创建的一个简单的刮板,用于将流式报价数据打印到控制台。它应该很容易转换为java。基于以下帖子:

    http://blog.underdog-projects.net/2009/02/bringing-the-yahoo-finance-stream-to-the-shell/

    不太花哨(即没有正则表达式等),只是一个快速而肮脏的解决方案。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web.Script.Serialization;
    
    namespace WebDataAddin
    {
        public class YahooConstants
        {
            public const string AskPrice = "a00";
            public const string BidPrice = "b00";
            public const string DayRangeLow = "g00";
            public const string DayRangeHigh = "h00";
            public const string MarketCap = "j10";
            public const string Volume = "v00";
            public const string AskSize = "a50";
            public const string BidSize = "b60";
            public const string EcnBid = "b30";
            public const string EcnBidSize = "o50";
            public const string EcnExtHrBid = "z03";
            public const string EcnExtHrBidSize = "z04";
            public const string EcnAsk = "b20";
            public const string EcnAskSize = "o40";
            public const string EcnExtHrAsk = "z05";
            public const string EcnExtHrAskSize = "z07";
            public const string EcnDayHigh = "h01";
            public const string EcnDayLow = "g01";
            public const string EcnExtHrDayHigh = "h02";
            public const string EcnExtHrDayLow = "g11";
            public const string LastTradeTimeUnixEpochformat = "t10";
            public const string EcnQuoteLastTime = "t50";
            public const string EcnExtHourTime = "t51";
            public const string RtQuoteLastTime = "t53";
            public const string RtExtHourQuoteLastTime = "t54";
            public const string LastTrade = "l10";
            public const string EcnQuoteLastValue = "l90";
            public const string EcnExtHourPrice = "l91";
            public const string RtQuoteLastValue = "l84";
            public const string RtExtHourQuoteLastValue = "l86";
            public const string QuoteChangeAbsolute = "c10";
            public const string EcnQuoteAfterHourChangeAbsolute = "c81";
            public const string EcnQuoteChangeAbsolute = "c60";
            public const string EcnExtHourChange1 = "z02";
            public const string EcnExtHourChange2 = "z08";
            public const string RtQuoteChangeAbsolute = "c63";
            public const string RtExtHourQuoteAfterHourChangeAbsolute = "c85";
            public const string RtExtHourQuoteChangeAbsolute = "c64";
            public const string QuoteChangePercent = "p20";
            public const string EcnQuoteAfterHourChangePercent = "c82";
            public const string EcnQuoteChangePercent = "p40";
            public const string EcnExtHourPercentChange1 = "p41";
            public const string EcnExtHourPercentChange2 = "z09";
            public const string RtQuoteChangePercent = "p43";
            public const string RtExtHourQuoteAfterHourChangePercent = "c86";
            public const string RtExtHourQuoteChangePercent = "p44";
    
            public static readonly IDictionary<string, string> CodeMap = typeof(YahooConstants).GetFields().
                Where(field => field.FieldType == typeof(string)).
                ToDictionary(field => ((string)field.GetValue(null)).ToUpper(), field => field.Name);
        }
    
        public static class StringBuilderExtensions
        {
            public static bool HasPrefix(this StringBuilder builder, string prefix)
            {
                return ContainsAtIndex(builder, prefix, 0);
            }
    
            public static bool HasSuffix(this StringBuilder builder, string suffix)
            {
                return ContainsAtIndex(builder, suffix, builder.Length - suffix.Length);
            }
    
            private static bool ContainsAtIndex(this StringBuilder builder, string str, int index)
            {
                if (builder != null && !string.IsNullOrEmpty(str) && index >= 0
                    && builder.Length >= str.Length + index)
                {
                    return !str.Where((t, i) => builder[index + i] != t).Any();
                }
                return false;
            }
        }
    
        public class WebDataAddin
        {
            public const string ScriptStart = "<script>";
            public const string ScriptEnd = "</script>";
    
            public const string MessageStart = "try{parent.yfs_";
            public const string MessageEnd = ");}catch(e){}";
    
            public const string DataMessage = "u1f(";
            public const string InfoMessage = "mktmcb(";
    
    
            protected static T ParseJson<T>(string json)
            {
                // parse json - max acceptable value retrieved from 
                //http://forums.asp.net/t/1343461.aspx
                var deserializer = new JavaScriptSerializer { MaxJsonLength = 2147483647 };
                return deserializer.Deserialize<T>(json);
            }
    
            public static void Main()
            {
                const string symbols = "GBPUSD=X,SPY,MSFT,BAC,QQQ,GOOG";
                // these are constants in the YahooConstants enum above
                const string attrs = "b00,b60,a00,a50";
                const string url = "http://streamerapi.finance.yahoo.com/streamer/1.0?s={0}&k={1}&r=0&callback=parent.yfs_u1f&mktmcb=parent.yfs_mktmcb&gencallback=parent.yfs_gencb&region=US&lang=en-US&localize=0&mu=1";
    
                var req = WebRequest.Create(string.Format(url, symbols, attrs));
                req.Proxy.Credentials = CredentialCache.DefaultCredentials;
                var missingCodes = new HashSet<string>();
                var response = req.GetResponse();
                if(response != null)
                {
                    var stream = response.GetResponseStream();
                    if (stream != null)
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            var builder = new StringBuilder();
                            var initialPayloadReceived = false;
                            while (!reader.EndOfStream)
                            {
                                var c = (char)reader.Read();
                                builder.Append(c);
                                if(!initialPayloadReceived)
                                {
                                    if (builder.HasSuffix(ScriptStart))
                                    {
                                        // chop off the first part, and re-append the
                                        // script tag (this is all we care about)
                                        builder.Clear();
                                        builder.Append(ScriptStart);
                                        initialPayloadReceived = true;
                                    }
                                }
                                else
                                {
                                    // check if we have a fully formed message
                                    // (check suffix first to avoid re-checking 
                                    // the prefix over and over)
                                    if (builder.HasSuffix(ScriptEnd) &&
                                        builder.HasPrefix(ScriptStart))
                                    {
                                        var chop = ScriptStart.Length + MessageStart.Length;
                                        var javascript = builder.ToString(chop,
                                            builder.Length - ScriptEnd.Length - MessageEnd.Length - chop);
    
                                        if (javascript.StartsWith(DataMessage))
                                        {
                                            var json = ParseJson<Dictionary<string, object>>(
                                                javascript.Substring(DataMessage.Length));
    
                                            // parse out the data. key should be the symbol
    
                                            foreach(var symbol in json)
                                            {
                                                Console.WriteLine("Symbol: {0}", symbol.Key);
                                                var symbolData = (Dictionary<string, object>) symbol.Value;
                                                foreach(var dataAttr in symbolData)
                                                {
                                                    var codeKey = dataAttr.Key.ToUpper();
                                                    if (YahooConstants.CodeMap.ContainsKey(codeKey))
                                                    {
                                                        Console.WriteLine("\t{0}: {1}", YahooConstants.
                                                            CodeMap[codeKey], dataAttr.Value);
                                                    } else
                                                    {
                                                        missingCodes.Add(codeKey);
                                                        Console.WriteLine("\t{0}: {1} (Warning! No Code Mapping Found)", 
                                                            codeKey, dataAttr.Value);
                                                    }
                                                }
                                                Console.WriteLine();
                                            }
    
                                        } else if(javascript.StartsWith(InfoMessage))
                                        {
                                            var json = ParseJson<Dictionary<string, object>>(
                                                javascript.Substring(InfoMessage.Length));
    
                                            foreach (var dataAttr in json)
                                            {
                                                Console.WriteLine("\t{0}: {1}", dataAttr.Key, dataAttr.Value);
                                            }
                                            Console.WriteLine();
                                        } else
                                        {
                                            throw new Exception("Cannot recognize the message type");
                                        }
                                        builder.Clear();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 您无需抓取即可获取数据。有一个完整的 API 可以为您提供漂亮的 CSV 文件:jarloo.com/yahoo_finance
    • 据我所知,JarLoo api 使用 Yahoo Finance 的 quotes.csv 端点,它不是流数据。上面的代码专门做实时报价数据的流式传输,没有轮询。 YQL 有一个实验性的流 API,但它在后台进行轮询
    【解决方案3】:

    您可以使用 YQL,但 yahoo.finance.* 表不是核心 yahoo 表。它是一个使用“csv api”并将其转换为 json 或 xml 格式的开放数据表。它使用起来更方便,但并不总是可靠的。我刚才无法使用它,因为它的表达到了存储限制或其他什么...

    您可以使用这个 php 库来使用 YQL 获取历史数据/报价 https://github.com/aygee/php-yql-finance

    【讨论】:

      【解决方案4】:

      Yahoo 非常易于使用并提供自定义数据。使用以下页面了解更多信息。

      finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT=pder=.csv

      警告 - 网络上有一些教程向您展示如何执行此操作,但如果您按照发布的方式使用股票代码所在的区域,则会导致错误。您将获得“缺少格式值”。我发现的教程省略了关于 GOOG 的评论。

      GOOG 的示例网址: http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,GOOG&f=nsl1op&e=.csv

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        相关资源
        最近更新 更多