Programming 板


LINE

各位大大好,第一次发文请见谅, 之前一直想学程式设计,但是一直没有行动, 直到上个月开始学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://webptt.com/cn.aspx?n=bbs/Programming/M.1406796459.A.905.html
1F:→ tomman0910:http://ppt.cc/IBTy附上参考档案位置^^ 59.124.7.114 07/31 16:52
2F:推 chlorine:我是用excel 的VBA来做 121.87.168.62 07/31 17:44
3F:→ chlorine:不过我正在研究怎麽用google app engine 121.87.168.62 07/31 17:44
4F:→ chlorine:有空一起交流吧 121.87.168.62 07/31 17:45
5F:推 yaupon:最近我也在做类似的事, 下载资料的部分是用 59.115.85.236 08/03 11:38
6F:→ yaupon:WebClient去做, 作法其实跟你的code差不多 59.115.85.236 08/03 11:39
7F:→ yaupon:http://ppt.cc/vomA,这是修改後的code, 59.115.85.236 08/03 11:41
8F:→ yaupon:上面的网址 要把逗号去掉 59.115.85.236 08/03 11:42
9F:→ tomman0910:谢谢两位^^我来认真研究一下CODE, 59.124.7.114 08/04 11:54
10F:→ tomman0910:看看之前到底错在哪边~~非常感谢 59.124.7.114 08/04 11:55







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP