public void QuerySinglePage(int page, string stockId) { Dictionary <int, StockItem> stockData = new Dictionary <int, StockItem>(); string[] xPaths = { "//*[@id='table2']/tr/td[1]/table", "//*[@id='table2']/tr/td[2]/table" }; DateTime receiveDate; HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlDocument docContext = new HtmlAgilityPack.HtmlDocument(); try { HttpWebRequest request = HttpWebRequest.Create(String.Format("http://bsr.twse.com.tw/bshtm/bsContent.aspx?StartNumber={0}&FocusIndex={1}", stockId, page)) as HttpWebRequest; request.Method = "GET"; request.Timeout = 300000; using (WebResponse response = request.GetResponse()) { doc.Load(response.GetResponseStream(), Encoding.UTF8); receiveDate = DateTime.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='receive_date']").InnerHtml.Trim()); if (page == 1) { using (stockdbaEntities db = new stockdbaEntities()) { // Basic Data if (db.DailySummary.Where(o => o.receiveDate == receiveDate && o.stockId == stockId).Count() == 0) { db.DailySummary.Add(new DailySummary() { receiveDate = receiveDate, stockId = doc.DocumentNode.SelectSingleNode("//*[@id='stock_id']").InnerHtml.Replace(" ", " ").Trim().Split(' ')[0], tradeRec = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='trade_rec']").InnerHtml.Replace(",", "").Trim()), tradeAmt = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='trade_amt']").InnerHtml.Replace(",", "").Trim()), tradeQty = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='trade_qty']").InnerHtml.Replace(",", "").Trim()), openPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='open_price']").InnerHtml.Trim()), highPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='high_price']").InnerHtml.Trim()), lowPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='low_price']").InnerHtml.Trim()), lastPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='last_price']").InnerHtml.Trim()) }); db.SaveChanges(); } } } foreach (var path in xPaths) { docContext.LoadHtml(doc.DocumentNode.SelectSingleNode(path).InnerHtml); HtmlNodeCollection nodeHeaders = docContext.DocumentNode.SelectNodes("./tr"); foreach (HtmlNode trItem in nodeHeaders) { if (trItem.Attributes["class"].Value.Contains("column_value_price")) { int serialNo; StockItem item = new StockItem(); HtmlNodeCollection tdItems = trItem.SelectNodes("./td"); if (int.TryParse(tdItems[0].InnerText.Trim(), out serialNo) == true) { item.No = serialNo; item.BrokerId = tdItems[1].InnerText.Trim().Split(' ')[0]; item.Value = float.Parse(tdItems[2].InnerText.Trim()); item.InVolume = int.Parse(tdItems[3].InnerText.Replace(",", "").Trim()); item.OutVolume = int.Parse(tdItems[4].InnerText.Replace(",", "").Trim()); stockData.Add(item.No, item); } } } } } // Add to DB if (stockData.Count != 0) { using (stockdbaEntities db = new stockdbaEntities()) { foreach (var item in stockData.Values) { if (db.DailyDetail.Where(o => o.receiveDate == receiveDate && o.stockId == stockId && o.no == item.No).Count() == 0) { db.DailyDetail.Add(new DailyDetail() { receiveDate = receiveDate, stockId = stockId, no = item.No, brokerId = item.BrokerId, value = (decimal)item.Value, buyVolume = item.InVolume, sellVolume = item.OutVolume }); //db.SaveChanges(); } } db.SaveChanges(); } } } catch (Exception ex) { using (stockdbaEntities db = new stockdbaEntities()) { db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = String.Format("QuerySinglePage:{0}", ex.Message) }); db.SaveChanges(); } throw; } }
public void QuerySinglePage(int page, string stockId) { Dictionary<int, StockItem> stockData = new Dictionary<int, StockItem>(); string[] xPaths = { "//*[@id='table2']/tr/td[1]/table", "//*[@id='table2']/tr/td[2]/table" }; DateTime receiveDate; HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlDocument docContext = new HtmlAgilityPack.HtmlDocument(); try { HttpWebRequest request = HttpWebRequest.Create(String.Format("http://bsr.twse.com.tw/bshtm/bsContent.aspx?StartNumber={0}&FocusIndex={1}", stockId, page)) as HttpWebRequest; request.Method = "GET"; request.Timeout = 300000; using (WebResponse response = request.GetResponse()) { doc.Load(response.GetResponseStream(), Encoding.UTF8); receiveDate = DateTime.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='receive_date']").InnerHtml.Trim()); if (page == 1) { using (stockdbaEntities db = new stockdbaEntities()) { // Basic Data if (db.DailySummary.Where(o => o.receiveDate == receiveDate && o.stockId == stockId).Count() == 0) { db.DailySummary.Add(new DailySummary() { receiveDate = receiveDate, stockId = doc.DocumentNode.SelectSingleNode("//*[@id='stock_id']").InnerHtml.Replace(" ", " ").Trim().Split(' ')[0], tradeRec = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='trade_rec']").InnerHtml.Replace(",", "").Trim()), tradeAmt = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='trade_amt']").InnerHtml.Replace(",", "").Trim()), tradeQty = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='trade_qty']").InnerHtml.Replace(",", "").Trim()), openPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='open_price']").InnerHtml.Trim()), highPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='high_price']").InnerHtml.Trim()), lowPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='low_price']").InnerHtml.Trim()), lastPrice = decimal.Parse(doc.DocumentNode.SelectSingleNode("//*[@id='last_price']").InnerHtml.Trim()) }); db.SaveChanges(); } } } foreach (var path in xPaths) { docContext.LoadHtml(doc.DocumentNode.SelectSingleNode(path).InnerHtml); HtmlNodeCollection nodeHeaders = docContext.DocumentNode.SelectNodes("./tr"); foreach (HtmlNode trItem in nodeHeaders) { if (trItem.Attributes["class"].Value.Contains("column_value_price")) { int serialNo; StockItem item = new StockItem(); HtmlNodeCollection tdItems = trItem.SelectNodes("./td"); if (int.TryParse(tdItems[0].InnerText.Trim(), out serialNo) == true) { item.No = serialNo; item.BrokerId = tdItems[1].InnerText.Trim().Split(' ')[0]; item.Value = float.Parse(tdItems[2].InnerText.Trim()); item.InVolume = int.Parse(tdItems[3].InnerText.Replace(",", "").Trim()); item.OutVolume = int.Parse(tdItems[4].InnerText.Replace(",", "").Trim()); stockData.Add(item.No, item); } } } } } // Add to DB if (stockData.Count != 0) { using (stockdbaEntities db = new stockdbaEntities()) { foreach (var item in stockData.Values) { if (db.DailyDetail.Where(o => o.receiveDate == receiveDate && o.stockId == stockId && o.no == item.No).Count() == 0) { db.DailyDetail.Add(new DailyDetail() { receiveDate = receiveDate, stockId = stockId, no = item.No, brokerId = item.BrokerId, value = (decimal)item.Value, buyVolume = item.InVolume, sellVolume = item.OutVolume }); //db.SaveChanges(); } } db.SaveChanges(); } } } catch (Exception ex) { using (stockdbaEntities db = new stockdbaEntities()) { db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = String.Format("QuerySinglePage:{0}", ex.Message) }); db.SaveChanges(); } throw; } }