Google Finance,如何获取 JSON 数据流?
我之前试图解释这一点,但显然失败了!
因此,如果您打开了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还需要更改示例中将
Connection
属性设置为Close
的行。注释掉这一行(同时将 keep-alive 属性设置为 true):您这样做,您的示例应该运行良好。
关于获取数据并使用 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
看起来像这样:
有点神秘,但是你可以看到 COLUMNS 线如何与底部的数据对齐。此外,
f
查询字符串参数似乎指示要返回哪些列(d=date、c=close、v=volume、o=open、h=high、l=low)。编辑:我应该提到,我使用的 URL 是从财务图表页面发送的,以获取更新的数据 - 您可以使用 Fiddler 等工具看到定期请求该 URL。我上面粘贴的响应数据也是由 MSDN 示例应用程序输出的。
但是,注释掉 MSDN 示例中的一行并稍微摆弄一下 Fiddler 应该可以为您提供解析来自该 URL 的返回所需的数据和线索。
我希望这有帮助!
PS - 我修改后的 MSDN 示例中的第一行如下所示:
我对示例中稍远一点的其他
WebRequest
调用进行了类似的更改...除此之外,我没有更改示例中的其他任何内容。You also need to change the example's line that sets the
Connection
property toClose
. Comment out this line (along with keeping the keep-alive property set to true):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:
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:
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.