Google Finance,如何获取 JSON 数据流?

发布于 2024-11-15 09:08:12 字数 557 浏览 4 评论 0原文

我之前试图解释这一点,但显然失败了!

因此,如果您打开了 Google 财经图表,例如:

http://www. google.com/finance?q=INDEXNASDAQ:.IXIC

我想以某种方式在 C# 中使用 (HttpWebRequest) 对象,以便我可以抓取 google 发送到页面的小数据来更新图表。

有朋友提到这是 JSON?

我试图使用下面的代码示例,但即使我将保持活动属性设置为“true”,它仍然无法工作:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx#Y369

I tried to explain this earlier, but obviously failed!

So, if you have a google finance graph open, for instance:

http://www.google.com/finance?q=INDEXNASDAQ:.IXIC

I would like to somehow use the (HttpWebRequest) object in C# so that I can grab the small data which google sends to the page to update the graph.

A friend mentioned this was JSON?

I was trying to use the following code example, but even when i set the keep alive property to 'true', it still wouldnt work:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx#Y369

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-11-22 09:08:12

您还需要更改示例中将 Connection 属性设置为 Close 的行。注释掉这一行(同时将 keep-alive 属性设置为 true):

myHttpWebRequest2.Connection = "Close";

您这样做,您的示例应该运行良好。

关于获取数据并使用 HttpWebRequest 来处理它,您可以这样做。返回的数据不是 JSON - 它看起来像纯文本,我猜 Google 的 javascript 正在解析它。 (我没有检查 Google Finance 页面上的 javascript,但这是我的猜测。)

使用 Fiddler,来自此 URL 的响应:

http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l& ;df=cpct&auto=1&ts=1307994768643

看起来像这样:

EXCHANGE%3DINDEXNASDAQ
MARKET_OPEN_MINUTE=570
MARKET_CLOSE_MINUTE=960
INTERVAL=120
COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME
DATA=
TIMEZONE_OFFSET=-240
a1307994120,2641.12,2641.12,2639.96,2640.01,0
1,2638.76,2642.14,2638.76,2641.13,0
2,2638.95,2640.54,2638.74,2638.79,0
3,2639.85,2640.01,2638.08,2638.95,0
4,2640.07,2640.87,2639.31,2639.88,0
5,2640.31,2640.48,2639.42,2640.08,0
6,2641.09,2641.09,2640.3,2640.31,0

有点神秘,但是你可以看到 COLUMNS 线如何与底部的数据对齐。此外,f 查询字符串参数似乎指示要返回哪些列(d=date、c=close、v=volume、o=open、h=high、l=low)。

编辑:我应该提到,我使用的 URL 是从财务图表页面发送的,以获取更新的数据 - 您可以使用 Fiddler 等工具看到定期请求该 URL。我上面粘贴的响应数据也是由 MSDN 示例应用程序输出的。

但是,注释掉 MSDN 示例中的一行并稍微摆弄一下 Fiddler 应该可以为您提供解析来自该 URL 的返回所需的数据和线索。

我希望这有帮助!

PS - 我修改后的 MSDN 示例中的第一行如下所示:

HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643");

我对示例中稍远一点的其他 WebRequest 调用进行了类似的更改...除此之外,我没有更改示例中的其他任何内容。

You also need to change the example's line that sets the Connection property to Close. Comment out this line (along with keeping the keep-alive property set to true):

myHttpWebRequest2.Connection = "Close";

You do that and your example should run fine.

Regarding getting the data and using HttpWebRequest to work with it, you can do that. The data returned isn't JSON - it looks like straight text and I'm guessing Google's javascript is parsing it out. (I haven't inspected the javascript on Google Finance's page, but that's my guess.)

Using Fiddler, the response from this URL:

http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643

looks like this:

EXCHANGE%3DINDEXNASDAQ
MARKET_OPEN_MINUTE=570
MARKET_CLOSE_MINUTE=960
INTERVAL=120
COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME
DATA=
TIMEZONE_OFFSET=-240
a1307994120,2641.12,2641.12,2639.96,2640.01,0
1,2638.76,2642.14,2638.76,2641.13,0
2,2638.95,2640.54,2638.74,2638.79,0
3,2639.85,2640.01,2638.08,2638.95,0
4,2640.07,2640.87,2639.31,2639.88,0
5,2640.31,2640.48,2639.42,2640.08,0
6,2641.09,2641.09,2640.3,2640.31,0

A little cryptic, but you can see how the COLUMNS line lines up with the data at the bottom. Also, the f querystring parameter seems to be indicating which columns to return (d=date, c=close,v=volume,o=open,h=high,l=low).

EDIT: I should mention that the URL I used is being sent from the finance graph page to get updated data - you can see this URL being requested at regular intervals using a tool like Fiddler. The response data that I pasted above is also output by the sample application from MSDN.

But commenting out that one line in the example from MSDN and a little fiddling with Fiddler should give you the data and clues you need to parse the return that comes from that URL.

I hope this helps!

PS - my first line in my modified MSDN example looks like this:

HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643");

I made a similar change to the other WebRequest call a little further down in the example...other than that, I didn't change anything else in the example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文