[問題] 想請問如何從twse下載資料

看板Programming作者 (tomman)時間11年前 (2014/07/31 16:47), 編輯推噓2(208)
留言10則, 3人參與, 最新討論串1/1
各位大大好,第一次發文請見諒, 之前一直想學程式設計,但是一直沒有行動, 直到上個月開始學c#跟visual studio2013 mvc到現在, 一直都很想要做一個程式來幫忙每天自動下載股市資料, 資料網站 http://ppt.cc/ukM1 大致想法是從csv檔讀取資料轉成dataStream然後在自己開的csv檔案中匯入, 因為還不太會所以從網站上找到一個很像的code來修改, 大致上用了wbRequest、Stream跟StreamReader, 不知道把程式碼貼在這邊可不可以QQ 因為已經不知道怎麼修改下載,懇請各位大大幫忙一下, using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace StockDataDownloader { class Program { static void Main(string[] args) { if (!Directory.Exists("D:/Stock/")) Directory.CreateDirectory("D:/Stock/"); FileStream fs = new FileStream("D:/Stock/" + 1123 + ".csv", FileMode.Create); StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding(950)); sw.WriteLine("日期", "發行量加權股價指數", "未含金融保險股指數", " 未含電子股指數", "未含金融電子股指數", "水泥類指數", "食品類指數", "塑膠類指數 ", "紡織纖維類指數", "電機機械類指數", "電器電纜類指數", "化學生技醫療類指數 ", "化學類指數", "生技醫療類指數", "玻璃陶瓷類指數", "造紙類指數", "鋼鐵類指數 ", "橡膠類指數", "汽車類指數", "電子類指數", "半導體類指數", "電腦及週邊設備類 指數", "光電類指數", "通信網路類指數", "電子零組件類指數", "電子通路類指數", " 資訊服務類指數", "其他電子類指數", "建材營造類指數", "航運類指數", "觀光類指數 ", "金融保險類指數", "貿易百貨類指數", "油電燃氣類指數", "其他類指數"); for (int y = 2013; y <= 2014; y++) { for (int m = 1; m <= 12; m++) { for (int d = 1; d <= 31; d++) { List<string> data = getData( y, m, d); //write month data to file for (int i = 0; i < data.Count; i++) { sw.WriteLine(data[i]); } Console.WriteLine(y + "/" + m + "/" + d + (data.Count == 0 ? " no data." : " download.")); } } } sw.Close(); fs.Close(); } static List<string> getData(int year, int month, int date) { //ref: http://msdn.microsoft.com/zh-tw/library/system.net.webrequest.aspx // Create a request for the URL. WebRequest request = WebRequest.Create("http://www.twse.com.tw/ch/trading/exchange/MI_5MINS_INDEX/MI_5MINS_INDEX_PD.php?genpage=genpage%2FReport" + year + month.ToString("mm") + "%2FA121" + year + month.ToString("mm") + date.ToString("dd") + ".php&type=csv"); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding(950)); // Read the content. List<string> data = new List<string>(); string title1 = reader.ReadLine(); if (title1 == null) //表示沒資料 { } else { reader.ReadLine(); while (!reader.EndOfStream) { data.Add(reader.ReadLine()); } data.RemoveAt(data.Count - 1); data.RemoveAt(data.Count - 1); } // Cleanup the streams and the response. reader.Close(); dataStream.Close(); response.Close(); return data; } } } 謝謝大大,如果有誤犯版規懇請告知。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.7.114 ※ 文章網址: http://www.ptt.cc/bbs/Programming/M.1406796459.A.905.html

07/31 16:52, , 1F
http://ppt.cc/IBTy附上參考檔案位置^^
07/31 16:52, 1F

07/31 17:44, , 2F
我是用excel 的VBA來做
07/31 17:44, 2F

07/31 17:44, , 3F
不過我正在研究怎麼用google app engine
07/31 17:44, 3F

07/31 17:45, , 4F
有空一起交流吧
07/31 17:45, 4F

08/03 11:38, , 5F
最近我也在做類似的事, 下載資料的部分是用
08/03 11:38, 5F

08/03 11:39, , 6F
WebClient去做, 作法其實跟你的code差不多
08/03 11:39, 6F

08/03 11:41, , 7F
http://ppt.cc/vomA,這是修改後的code,
08/03 11:41, 7F

08/03 11:42, , 8F
上面的網址 要把逗號去掉
08/03 11:42, 8F

08/04 11:54, , 9F
謝謝兩位^^我來認真研究一下CODE,
08/04 11:54, 9F

08/04 11:55, , 10F
看看之前到底錯在哪邊~~非常感謝
08/04 11:55, 10F
文章代碼(AID): #1JsWAha5 (Programming)
文章代碼(AID): #1JsWAha5 (Programming)