[问题] 想请问如何从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