如何从Yahoo Finance这样的网站获取数据?

时间:2011-12-02 19:33:15

标签: php html xml web-scraping yahoo-finance

我有一个需要帮助的项目。我希望能够访问我制作的网站,例如abc123.com,让它显示道琼斯指数。而已。只需在我的abc123.com网站上显示一个数字,如12050。

显然我必须从Yahoo Finance这样的地方获取数据。我不想下载任何.csv文件或类似的东西。而且它不需要是实时数据,因为我知道雅虎的数据延迟了15分钟。只是希望能够访问我自己的网站,看看道琼斯指数是什么。

在提出这个问题之前,我确实在SO上研究了一些类似的问题,但是其中很多都没有得到答复或引用了YQL(我正在研究并试图理解这一点)。

任何有关最简单方法的指导都将非常感谢!我是HTML的初级程序员,我正在寻找最容易理解和实现的路线。

4 个答案:

答案 0 :(得分:4)

你做不到。不是以编程方式,而是合法的。

除非另有说明,否则报价会延迟。纳斯达克,纽约证券交易所和美国证券交易所的延迟时间为15分钟。另见其他交易所的延迟时间。雅虎上发现的独立提供商提供的报价和其他信息!财务合作伙伴页面报价会自动更新,但会在25分钟不活动后关闭。行情延迟至少15分钟。所有信息均“按原样”提供,仅供参考,不用于交易目的或建议。雅虎都不是!任何独立提供商均不对任何信息错误,不完整或延迟,或依赖此处包含的信息采取的任何行动负责。 访问Yahoo!网站,您同意不再重新分发其中的信息。

所有主要的市场数据提供商都有某种合法的东西。

但是如果你打算这样做,我可能会下载html并解析它以找到你正在寻找的报价。

如果您想合法地执行此操作,您将不得不花费大量资金从交易所本身或传递信息的第三方实体获取数据。在这种情况下,您将不得不使用它们提供的任何API。

消息来源:尝试做你正在考虑的事情:)

(EDIT)对于那些使用YQL的人:

查看使用条款。

你不能:

出售,租赁,分享,转让或再许可YQL或与YQL一起使用YQL获得收入。 API或其他网络服务,无论是出于直接商业利益还是其他方式,未经雅虎事先明确的书面许可

在与Yahoo提供的产品或服务竞争的产品或服务中使用YQL!

任何分享股票报价的网站都将与finance.yahoo.com竞争,因此不遵守YPL的使用条款。此外,您将无法从使用此数据的工作中赚取任何金钱。祝你明确书面同意。

在YQL上的“The Dark Side”here下阅读:“谁拥有我的代码?从技术上讲,他们拥有一切。”

在可扩展性,盈利性,简单性或合法性方面,YQL不是一个好的解决方案。如果你不关心任何这些东西,你的网络应用程序不会受欢迎或赚钱,你不介意写一堆代码与雅虎玩得很好!那么YQL就是你的选择。

(编辑#2)

如果您只是想为自己显示信息(不与他人共享),我发现解析html并在使用C#/ Visual Basic和ASP.NET而不是php的网站上显示它非常容易。您可以在C#/ VB中创建一个http请求,它将(最终)返回一个html文件,您可以解析或搜索道琼斯指数报价。在C#/ VB中执行此操作作为ASP Web应用程序的后端。我相信您可以使用免费的Visual Studio 2010 Express创建这些。

答案 1 :(得分:1)

我不确定这样做是否有法律限制。有很多商业(付费)应用从雅虎财经中提取数据,并且多年来一直运作良好。我要说的是,这些应用程序安装在用户的设备(iOS / Android / Windows设备)中,因此他们从Yahoo Finance中提取数据供用户自己使用。

也许如果您通过Javascript从Yahoo Finance中提取数据并且未将其存储在您的服务器中,则同样的情况也适用于您的项目。事实上,我实际上是这么认为的。这种情况的唯一区别是您的代码不需要安装在用户的设备上,因为它是一个网页。

您是否曾尝试联系YQL或Yahoo Finance的支持,以讨论项目的法律限制?

答案 2 :(得分:1)

我写了一个从Yahoo!获取股票数据的函数。财务API;但是,我的代码下载.csv文件。我希望它有用。

完整的源代码和示例结果位于my blog

<?php

/*
Description: Getting Stock Data from Yahoo! Finance
Author URI: http://phpvancouver.ca/
*/

/* 
* More about Yahoo! Finance Tag
* http://www.gummy-stuff.org/Yahoo-data.htm
* http://www.canbike.ca/information-technology/2013/08/10/yahoo-finance-url-download-to-a-csv-file.html
*/

$yahoo_finance_tags = array(
"a" => "Ask",       "a2" => "Average Daily Volume",     "a5" => "Ask Size",
"b" => "Bid",       "b2" => "Ask (Real-time)",      "b3" => "Bid (Real-time)",
"b4" => "Book Value",       "b6" => "Bid Size",     "c" => "Change & Percent Change",
"c1" => "Change",       "c3" => "Commission",       "c6" => "Change (Real-time)",
"c8" => "After Hours Change (Real-time)",       "d" => "Dividend/Share",        "d1" => "Last Trade Date",
"d2" => "Trade Date",       "e" => "Earnings/Share",        "e1" => "Error Indication (returned for symbol changed / invalid)",
"e7" => "EPS Estimate Current Year",        "e8" => "EPS Estimate Next Year",       "e9" => "EPS Estimate Next Quarter",
"f6" => "Float Shares",     "g" => "Day's Low",     "h" => "Day's High",
"j" => "52-week Low",       "k" => "52-week High",      "g1" => "Holdings Gain Percent",
"g3" => "Annualized Gain",      "g4" => "Holdings Gain",        "g5" => "Holdings Gain Percent (Real-time)",
"g6" => "Holdings Gain (Real-time)",        "i" => "More Info",     "i5" => "Order Book (Real-time)",
"j1" => "Market Capitalization",        "j3" => "Market Cap (Real-time)",       "j4" => "EBITDA",
"j5" => "Change From 52-week Low",      "j6" => "Percent Change From 52-week Low",      "k1" => "Last Trade (Real-time) With Time",
"k2" => "Change Percent (Real-time)",       "k3" => "Last Trade Size",      "k4" => "Change From 52-week High",
"k5" => "Percebt Change From 52-week High",     "l" => "Last Trade (With Time)",        "l1" => "Last Trade (Price Only)",
"l2" => "High Limit",       "l3" => "Low Limit",        "m" => "Day's Range",
"m2" => "Day's Range (Real-time)",      "m3" => "50-day Moving Average",        "m4" => "200-day Moving Average",
"m5" => "Change From 200-day Moving Average",       "m6" => "Percent Change From 200-day Moving Average",       "m7" => "Change From 50-day Moving Average",
"m8" => "Percent Change From 50-day Moving Average",        "n" => "Name",      "n4" => "Notes",
"o" => "Open",      "p" => "Previous Close",        "p1" => "Price Paid",
"p2" => "Change in Percent",        "p5" => "Price/Sales",      "p6" => "Price/Book",
"q" => "Ex-Dividend Date",      "r" => "P/E Ratio",     "r1" => "Dividend Pay Date",
"r2" => "P/E Ratio (Real-time)",        "r5" => "PEG Ratio",        "r6" => "Price/EPS Estimate Current Year",
"r7" => "Price/EPS Estimate Next Year",     "s" => "Symbol",        "s1" => "Shares Owned",
"s7" => "Short Ratio",      "t1" => "Last Trade Time",      "t6" => "Trade Links",
"t7" => "Ticker Trend",     "t8" => "1 yr Target Price",        "v" => "Volume",
"v1" => "Holdings Value",       "v7" => "Holdings Value (Real-time)",       "w" => "52-week Range",
"w1" => "Day's Value Change",       "w4" => "Day's Value Change (Real-time)",       "x" => "Stock Exchange",
);

/* This function gets a symbol or an array of symbol as a parameter.
* And it returns an array of the corresponding stock data.
* If an error occurs, the detail of the error is saved in $error_message variable
* which is passed by reference from the parent function
*/

function get_stock_data_from_yahoo_finance_pv($symbol, &$error_message) {

global $yahoo_finance_tags;
$error_message = NULL; // Default value 

$f = ""; // The f parameter in Yahoo! Finance URL
foreach($yahoo_finance_tags as $key => $value)
    $f = $f . $key;

if ( is_array($symbol) ) { // if the symbol is an array

    if ( $symbol == NULL ) { // if the symbol is invalid
        $error_message = "The given symbol is invalid.";
        return -1; // ERROR
    }

    $url = "http://finance.yahoo.com/d/quotes.csv?s=" . implode("+", $symbol) . "&f=" . $f;
    $fp = @fopen($url, "r");
    if ( $fp == FALSE ) { // If the URL can't be opened
        $error_message = "Cannot get data from Yahoo! Finance. The following URL is not accessible, $url";
        return -1; // ERROR
    }

    $arr = array();
    $symbol = explode("+",implode("+", $symbol)); // Eliminate the keys in the symbol array
    $j = 0;
    while ( ($array = @fgetcsv($fp , 4096 , ', ')) !== FALSE ) {
        $i = 0;
        $p = array();
        foreach($yahoo_finance_tags as $key => $value) {
            $p[$key] = $array[$i];
            $i = $i + 1;
        }
        $arr[$symbol[$j]]= $p;
        $j = $j + 1;
    }
    @fclose($fp);
    return $arr;

} else {  // if the symbol is not array 

    if ( strlen($symbol) < 1 || $symbol == NULL ) { // if the symbol is invalid
        $error_message = "The given symbol is invalid.";
        return -1; // ERROR
    }

    $url = "http://finance.yahoo.com/d/quotes.csv?s=" . $symbol . "&f=" . $f;
    $fp = @fopen($url, "r");
    if ( $fp == FALSE ) { // If the URL can't be opened
        $error_message = "Cannot get data from Yahoo! Finance. The following URL is not accessible, $url";
        return -1; // ERROR
    }

    $array = @fgetcsv($fp , 4096 , ', '); 
    $arr = array();
    $i = 0;
    foreach($yahoo_finance_tags as $key => $value) {
        $arr[$key] = $array[$i];
        $i = $i + 1;
    }
    @fclose($fp);       
    return $arr;
}
return -1;
}

答案 3 :(得分:0)

感谢所有贡献的人,因为在这个主题上产生的想法帮助我找到了答案,并且完全正确地找到了我想要的东西。这个人解释了如何用大约10行代码来完成它:

http://www.seangw.com/wordpress/index.php/2010/01/formatting-stock-data-from-yahoo-finance/

基本上我所做的一切都是切割&amp;粘贴他的代码并将其放在我的网站上的index.html中。访问我的网站时,我得到了我感兴趣的代码报价。您可以修改$ stocklist以存入您想要的任何股票,并且您可以使用$ stockFormat中的代码修改Yahoo的响应。再次感谢SO!