1. 程式人生 > >實時股票和天氣預報

實時股票和天氣預報

實時股票

Google 的資料

1. 實時資料 http://www.google.com/ig/api?h1=zh-CN&stock=600001

2.K線圖 http://www.google.cn/finance/getchart?q=600336&x=SHA&p=5d&i=240 http://www.google.com/finance/chart?q=SHA:600001&tlf=12

3. 補全資訊 "http://www.google.cn/finance/match?matchtype=matchall&q=600".

Yahoo 的資料

1. 實時資料 http://download.finance.yahoo.com/d/quotes.csv?s=600336.SS&f=sl1d1t1c1ohgv&e=.csv

2. K線圖 http://ichart.finance.yahoo.com/t?s=600036.ss

3. K線圖資料 http://chartapi.finance.yahoo.com/instrument/1.0/000001.ss/chartdata;type=quote;range=3m

天氣預報

Google Weather API 只支援美國地區使用郵政編碼進行查詢,例如: http://www.google.com/ig/api?hl=zh-cn&weather=94043 (94043 為 山景城, 美國加州 的郵政編碼) 而除了美國以外的地區需要使用經緯度座標作為引數才能執行 Google Weather API, 例如: http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996 (30670000,104019996 為 成都, 中國大陸 的經緯度座標)

當然,也可能通行城市名稱的漢語拼音來查詢,例如:以下是北京的天氣

http://www.google.com/ig/api?weather=Beijing

要其它地區的經緯度座標,可以通過 Google API 提供的國家程式碼列表及相應的城市經緯度座標列表可以查詢到,以下是 Google API 提供的查詢引數: http://www.google.com/ig/countries?output=xml&hl=zh-cn (查詢 Google 所支援的所有國家的程式碼,並以 zh-cn 簡體中文顯示) http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn

Google Weather API 只支援美國地區使用郵政編碼進行查詢,例如: http://www.google.com/ig/api?hl=zh-cn&weather=94043 (94043 為 山景城, 美國加州 的郵政編碼) 而除了美國以外的地區需要使用經緯度座標作為引數才能執行 Google Weather API, 例如: http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996 (30670000,104019996 為 成都, 中國大陸 的經緯度座標)

當然,也可能通行城市名稱的漢語拼音來查詢,例如:以下是北京的天氣

http://www.google.com/ig/api?weather=Beijing

要其它地區的經緯度座標,可以通過 Google API 提供的國家程式碼列表及相應的城市經緯度座標列表可以查詢到,以下是 Google API 提供的查詢引數: http://www.google.com/ig/countries?output=xml&hl=zh-cn (查詢 Google 所支援的所有國家的程式碼,並以 zh-cn 簡體中文顯示) http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn

C#範例:

using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Net;

///Code by Roman Alifanov - API by Google ///Posted on http://animaonline.blogspot.com namespace GoogleWeatherAPI_Parser { class Program { static void Main(string[] args)

 { int i = 0;

     XmlNodeList GWP_NodeList = GoogleWeatherAPI_Parser(@"http://www.google.co.uk/ig/api?weather=Drammen").SelectNodes("xml_api_reply/weather/current_conditions"); Console.WriteLine("Current weather in Drammen: " + GWP_NodeList.Item(i).SelectSingleNode("temp_c").Attributes["data"].InnerText); Console.ReadLine(); }

 private static XmlDocument GoogleWeatherAPI_Parser(string baseUrl)

 { HttpWebRequest GWP_Request;

     HttpWebResponse GWP_Response = null;

     XmlDocument GWP_XMLdoc = null;

     try

     { GWP_Request = (HttpWebRequest)WebRequest.Create(string.Format(baseUrl)); GWP_Request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4"; GWP_Response = (HttpWebResponse)GWP_Request.GetResponse();

         GWP_XMLdoc = new XmlDocument();

         GWP_XMLdoc.Load(GWP_Response.GetResponseStream()); }

     catch (Exception ex)

     { Console.WriteLine(ex.Message); }

     GWP_Response.Close();

     return GWP_XMLdoc; }

 } }

附:其它天氣API

Yahoo:http://developer.yahoo.com/weather/

北京天氣(可以通過搜尋查詢)

http://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=f

國內API:

北京天氣

http://weather.all2rss.com/weatherrss.asp?City=北京