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

作者: tomman0910 (tomman)   2014-07-31 16:47:33
各位大大好,第一次發文請見諒,
之前一直想學程式設計,但是一直沒有行動,
直到上個月開始學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;
}
}
}
謝謝大大,如果有誤犯版規懇請告知。

Links booklink

Contact Us: admin [ a t ] ucptt.com