php 如何从 Yahoo Finance 等网站获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8361582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get data from a site like Yahoo Finance?
提问by phan
I have a project that I need help with. I want to be able to go to a website of my making, e.g. abc123.com, and have it display the Dow Jones Index. That's it. Just display one number like 12050 on my abc123.com website.
我有一个项目需要帮助。我希望能够访问我创建的网站,例如 abc123.com,并让它显示道琼斯指数。就是这样。只需在我的 abc123.com 网站上显示一个数字,例如 12050。
Obviously I have to get the data from somewhere like Yahoo Finance. I don't want to download any .csv files or anything like that. And it doesn't need to be real time data since I know Yahoo's data is 15 minutes delayed. Just want to be able to visit my own site and see what the Dow Jones Index is.
显然,我必须从诸如雅虎财经之类的地方获取数据。我不想下载任何 .csv 文件或类似的文件。而且它不需要是实时数据,因为我知道雅虎的数据延迟了 15 分钟。只是希望能够访问我自己的网站,看看道琼斯指数是什么。
Before asking this question I did research some similar questions on SO, but a lot of them were unanswered or references YQL (something I'm looking into and trying to comprehend at this moment).
在问这个问题之前,我确实研究了一些关于 SO 的类似问题,但其中很多都没有得到解答或引用了 YQL(我目前正在研究并试图理解的东西)。
Any guidance on the simplest way to do this would be greatly appreciated! I am a beginning programmer in HTML and am looking for the easiest-to-understand-and-implement route.
任何有关执行此操作的最简单方法的指导将不胜感激!我是 HTML 的初级程序员,正在寻找最容易理解和实现的路线。
采纳答案by Andrew Rasmussen
You can't. Not programmatically, but legally.
你不能。不是以编程方式,而是合法地。
Quotes delayed, except where indicated otherwise. Delay times are 15 mins for NASDAQ, NYSE and Amex. See also delay times for other exchanges. Quotes and other information supplied by independent providers identified on the Yahoo! Finance partner page. Quotes are updated automatically, but will be turned off after 25 minutes of inactivity. Quotes are delayed at least 15 minutes. All information provided "as is" for informational purposes only, not intended for trading purposes or advice. Neither Yahoo! nor any of independent providers is liable for any informational errors, incompleteness, or delays, or for any actions taken in reliance on information contained herein. By accessing the Yahoo! site, you agree not to redistribute the information found therein.
报价有延时,除非特别注明。NASDAQ、NYSE 和 Amex 的延迟时间为 15 分钟。另请参阅其他交易所的延迟时间。由 Yahoo! 上标识的独立提供商提供的报价和其他信息 财务合作伙伴页面。报价会自动更新,但会在 25 分钟不活动后关闭。报价至少延迟 15 分钟。所有“按原样”提供的信息仅供参考,不用于交易目的或建议。无论是雅虎!任何独立供应商均不对任何信息错误、不完整或延迟,或因依赖此处包含的信息而采取的任何行动负责。通过访问雅虎!网站,您同意不重新分发其中的信息。
All of the major market data providers have some sort of legal stuff like that.
所有主要的市场数据提供商都有类似的法律规定。
But if you were going to do it anyways, I would probably download the html and parse it in order to find the quote you're looking for.
但是,如果您无论如何都要这样做,我可能会下载 html 并对其进行解析以找到您正在寻找的报价。
If you want to do it legally you're going to have to pay a lot of money to get the data from either the exchange itself or a third party entity that relays the information. In that case you're going to have to use whatever API they provide.
如果你想合法地这样做,你将不得不支付很多钱才能从交易所本身或中继信息的第三方实体获取数据。在这种情况下,您将不得不使用他们提供的任何 API。
Sources: trying to do what you're thinking about doing :)
资料来源:尝试做你想做的事情:)
(EDIT) To those who say use YQL:
(编辑)对于那些说使用 YQL 的人:
Take a look at the Terms of Use.
查看使用条款。
You can not:
你不能:
Sell, lease, share, transfer, or sublicense YQL or derive income from the use of YQL in conjunction with Yahoo! APIs or other web services, whether for direct commercial or monetary gain or otherwise, without Yahoo!'s prior, express, written permission
出售、租赁、共享、转让或再许可 YQL 或从与 Yahoo! 一起使用 YQL 中获得收入!未经 Yahoo! 事先明确书面许可,API 或其他网络服务,无论是为了直接商业或金钱收益还是其他目的
or
或者
Use YQL in a product or service that competes with products or services offered by Yahoo!
在与 Yahoo! 提供的产品或服务竞争的产品或服务中使用 YQL
Any site that is sharing stock quotes is going to be competing with finance.yahoo.com and therefore disobeying the terms of use of YPL. Furthermore, you won't be able to make any money off of what you're doing with this data. Good luck getting express written consent.
任何共享股票报价的网站都将与 Finance.yahoo.com 竞争,从而违反 YPL 的使用条款。此外,您将无法从使用这些数据所做的事情中获利。祝您获得明确的书面同意。
Read under "The Dark Side" hereon YQL too: "who owns my code? Technically, they own everything."
仔细阅读“月之暗面”这里的YQL太:“谁拥有我的代码从技术上讲,他们所拥有的一切。”
YQL isn't a good solution in terms of scalability, profitability, simplicity, or legality. If you don't care about any of these things and your web application isn't going to be popular or make you money, and you don't mind writing a bunch of code to play nicely with Yahoo! then YQL is the way to go.
YQL 在可扩展性、盈利能力、简单性或合法性方面并不是一个好的解决方案。如果您不关心这些事情中的任何一个,并且您的 Web 应用程序不会流行或不会让您赚钱,并且您不介意编写一堆代码来与 Yahoo! 那么YQL是要走的路。
(EDIT #2)
(编辑#2)
If you are just trying to display the information for yourself (not sharing it with others), I find it extremely easy to parse html and display it on a website with C#/Visual Basic and ASP.NET rather than php. You can create an http request in C#/VB which will (eventually) return an html file, which you can parse or search through for your Dow Jones Index quote. Do this all in C#/VB as the back end of an ASP web application. I believe you can create these with Visual Studio 2010 Express which is free.
如果您只是想为自己显示信息(而不是与他人共享),我发现解析 html 并将其显示在使用 C#/Visual Basic 和 ASP.NET 而不是 php 的网站上非常容易。您可以在 C#/VB 中创建一个 http 请求,该请求将(最终)返回一个 html 文件,您可以通过该文件解析或搜索道琼斯指数报价。在作为 ASP Web 应用程序后端的 C#/VB 中执行所有这些操作。我相信您可以使用免费的 Visual Studio 2010 Express 创建这些。
回答by user1565668
I am not sure whether there's a legal limit for doing this. There are lots of commercial (paid) apps that pull data from Yahoo Finance, and have been working fine for years. I'd say that the trick here is that these apps are installed into the user's device (iOS/Android/Windows devices), and therefore they pull data from Yahoo Finance for the user's own personal use.
我不确定这样做是否有法律限制。有许多商业(付费)应用程序从雅虎财经中提取数据,并且多年来一直运行良好。我想说的是,这里的技巧是这些应用程序安装在用户的设备(iOS/Android/Windows 设备)中,因此它们从雅虎财经中提取数据供用户个人使用。
Maybe if you pulled the data from Yahoo Finance through Javascript and didn't store it in your servers, the same case would apply to your project. As a matter of fact, I would actually think so. The only difference in this case is that your code would not need to be installed on the user's device, as it is a web page.
也许如果您通过 Javascript 从 Yahoo Finance 中提取数据并且没有将其存储在您的服务器中,那么同样的情况将适用于您的项目。事实上,我真的会这么认为。在这种情况下唯一的区别是您的代码不需要安装在用户的设备上,因为它是一个网页。
Have you tried to contact YQL or Yahoo Finance's support to discuss the legal limits of your projects?
您是否尝试联系 YQL 或 Yahoo Finance 的支持以讨论您项目的法律限制?
回答by Moh
I've written a function to get stock data from Yahoo! Finance API; however, my code downloads .csv file. I hope it's useful.
我编写了一个函数来从 Yahoo! 获取股票数据。金融API;但是,我的代码下载 .csv 文件。我希望它有用。
The full source code and sample results are on 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;
}
回答by phan
Thanks to everyone who contributed because the ideas generated on this thread helped me to find an answer and do EXACTLY what I was looking for. This guy explains how to do it in roughly 10 lines of code :
感谢所有做出贡献的人,因为在此线程上产生的想法帮助我找到了答案并完全按照我的要求去做。这家伙用大约 10 行代码解释了如何做到这一点:
http://www.seangw.com/wordpress/index.php/2010/01/formatting-stock-data-from-yahoo-finance/
http://www.seangw.com/wordpress/index.php/2010/01/formatting-stock-data-from-yahoo-finance/
So basically all I did was cut & paste his code and put it in index.html on my site. When visiting my website I get the quotes of tickers I'm interested in. You can modify $stocklist to put in whatever stock you want, and you can modify the response from Yahoo using the tags in $stockFormat. Thanks again SO!
所以基本上我所做的就是剪切并粘贴他的代码并将其放在我网站上的 index.html 中。当访问我的网站时,我会得到我感兴趣的股票报价。您可以修改 $stocklist 以放入您想要的任何股票,并且您可以使用 $stockFormat 中的标签修改来自雅虎的响应。再次感谢!