public void GetIndiStockData(string vstrStockCode, string vstrDate,string vstrEndDate) { string strYear; string strMonth; List<string> ListDataMonth = new List<string>(); List<StockIndi> LiTotal = new List<StockIndi>(); ClsBaseFunction CBF = new ClsBaseFunction(); DataTable TotalDt = new DataTable(); DAO clsDao = new DAO(); ClsGetWebData ClsGW = new ClsGetWebData(); ListDataMonth = CBF.GetDateRoute(vstrDate,vstrEndDate); foreach (string str in ListDataMonth) { //Para1:strYear //Para2:steMonth //Para3:vstrStocCode strYear = str.Substring(0, 4); strMonth = str.Substring(4, 2); ClsGW.Para1 = strYear; ClsGW.Para2 = strMonth; ClsGW.Para3 = vstrStockCode; ClsGW.GetIndiStockWebData(); //OutputList.AddRange(ClsGW.OutLiStockIndi); LiTotal.AddRange(ClsGW.OutLiStockIndi); } OutputList = LiTotal; //return ClsGW.OutLiStockIndi; }
public void GetIndiStockWebData() { try { List<StockIndi> LiStockIndi = new List<StockIndi>(); //DAO ClsDao = new DAO(); //ClsDao.CnStringCode = "connstring3"; string strWebSite; strWebSite = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/genpage/Report" + Para1 + Para2 + "/" + Para1 + Para2 + "_F3_1_8_" + Para3 + ".php?STK_NO=" + Para3 + "&myear=" + Para1 + "&mmon=" + Para2; WebClient url = new WebClient(); MemoryStream ms = new MemoryStream(url.DownloadData(strWebSite)); HtmlDocument doc = new HtmlDocument(); ClsBaseFunction CBF = new ClsBaseFunction(); doc.Load(ms, Encoding.Default); HtmlDocument hdc = new HtmlDocument(); foreach (var cell in doc.DocumentNode.SelectNodes("/html[1]/body[1]/table[1]/tr[3]/td[1]/table[3]/tr[position()>2]")) { LiStockIndi.Add( new StockIndi { TxnDate = Convert.ToDateTime(CBF.TWNTransADDate(cell.SelectSingleNode("td[1]").InnerText)).ToShortDateString(), DealStockCnt = Convert.ToInt32(cell.SelectSingleNode("td[2]").InnerText.Replace(",", "")), DealStockAmt = Convert.ToInt64(cell.SelectSingleNode("td[3]").InnerText.Replace(",", "")), OpenPrice = Convert.ToDecimal(cell.SelectSingleNode("td[4]").InnerText), HighPrice = Convert.ToDecimal(cell.SelectSingleNode("td[5]").InnerText), LowPrice = Convert.ToDecimal(cell.SelectSingleNode("td[6]").InnerText), ClosePrice = Convert.ToDecimal(cell.SelectSingleNode("td[7]").InnerText), PriceDiff = cell.SelectSingleNode("td[8]").InnerText, DealCount = Convert.ToInt32(cell.SelectSingleNode("td[9]").InnerText.Replace(",", "")) }); } OutLiStockIndi = LiStockIndi; } catch (WebException) { } catch (Exception) { //throw; } }
private HtmlDocument HttpGetDataDoc(string vstrUrl) { WebClient url = new WebClient(); MemoryStream ms = new MemoryStream(url.DownloadData(vstrUrl)); HtmlDocument doc = new HtmlDocument(); ClsBaseFunction CBF2 = new ClsBaseFunction(); doc.Load(ms, Encoding.Default); return doc; }
public void GetTWNIndex() { try { string strUrl; strUrl = "http://www.twse.com.tw/ch/trading/indices/MI_5MINS_HIST/MI_5MINS_HIST.php"; List<TWNInd> LiTWInd = new List<TWNInd>(); WebClient url = new WebClient(); MemoryStream ms = new MemoryStream(url.DownloadData(strUrl)); HtmlDocument doc = new HtmlDocument(); ClsBaseFunction CBF = new ClsBaseFunction(); doc.Load(ms, Encoding.Default); HtmlDocument hdc = new HtmlDocument(); ASCIIEncoding encoding = new ASCIIEncoding(); string aa = (Convert.ToInt32(Para1)-1911).ToString(); string bb = Para2; string PostData; PostData = ""; PostData += "myear=" + aa; PostData += "&mmon=" + bb; byte[] data = encoding.GetBytes(PostData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); doc.Load(reader); foreach (var cell in doc.DocumentNode.SelectNodes("/html[1]/body[1]/table[1]/tr[3]/td[1]/table[3]/tr[position()>2]")) { LiTWInd.Add( new TWNInd { TxnDate = Convert.ToDateTime(CBF.TWNTransADDate(cell.SelectSingleNode("td[1]").InnerText)).ToShortDateString(), OpenPrice = Convert.ToDecimal(cell.SelectSingleNode("td[2]").InnerText.Replace(",", "")), HighPrice = Convert.ToDecimal(cell.SelectSingleNode("td[3]").InnerText.Replace(",", "")), LowPrice = Convert.ToDecimal(cell.SelectSingleNode("td[4]").InnerText.Replace(",", "")), ClosePrice = Convert.ToDecimal(cell.SelectSingleNode("td[5]").InnerText.Replace(",", "")) }); } strUrl = "http://www.twse.com.tw/ch/trading/exchange/FMTQIK/genpage/Report" + Para1 + Para2 + "/" + Para1 + Para2 + "_F3_1_2.php?STK_NO=&myear=" + Para1 + "&mmon=" + Para2; HtmlDocument doc2 = HttpGetDataDoc(strUrl); int z = 0; foreach (var cell in doc2.DocumentNode.SelectNodes("/html[1]/body[1]/table[1]/tr[3]/td[1]/table[3]/tr[position()>2]")) { LiTWInd[z].DealAmt = Convert.ToInt64(cell.SelectSingleNode("td[3]").InnerText.Replace(",", "")); z += 1; } OutLiTWNInd = LiTWInd; } catch { } }