private async Task <bool> ParseHtml(string url, bool isTwse) { try { string firstResponse = await GlobalUtility.GetHtmlAsync(url, System.Text.Encoding.Default);//EDLib.Utility.GetHtml(url, System.Text.Encoding.Default); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(firstResponse); HtmlNodeCollection navNodeChild = doc.DocumentNode.SelectSingleNode("//table[1]").ChildNodes; // /html[1]/body[1]/center[1]/table for (int i = 5; i < navNodeChild.Count; i += 2) { string[] split = navNodeChild[i].InnerText.Split(new string[] { " ", "\t", " ", "\n" }, StringSplitOptions.RemoveEmptyEntries); DataRow dr = dt.NewRow(); dr["WarrantID"] = split[0]; dr["WarrantName"] = split[1]; dr["IssueNum"] = split[2]; dr["SoldNum"] = split[3]; dr["Last1Sold"] = split[4]; dr["Last2Sold"] = split[5]; dr["Last3Sold"] = split[6]; dr["LastTradingDate"] = split[7]; dr["ReIssuable"] = split[8]; dt.Rows.Add(dr); } return(true); } catch (WebException) { if (isTwse) { MessageBox.Show("可能要更新Key,或是網頁有問題"); } return(false); } catch (NullReferenceException) { if (isTwse) { MessageBox.Show("TWSE 沒有資料"); } else { MessageBox.Show("OTC 沒有資料"); } return(false); } catch (Exception e) { MessageBox.Show(e.ToString()); return(false); } }
private async Task <bool> ParseHtml(string url, bool isTwse) { try { string firstResponse = await GlobalUtility.GetHtmlAsync(url, Encoding.Default); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(firstResponse); HtmlNodeCollection navNodeChild = doc.DocumentNode.SelectSingleNode("//table[1]/tr[1]/td/table").ChildNodes; for (int i = 3; i < navNodeChild.Count; i += 2) { string[] split = navNodeChild[i].InnerText.Split(new string[] { " ", "\t", " ", "\n" }, StringSplitOptions.RemoveEmptyEntries); ultraDataSource1.Rows.Add(); ultraDataSource1.Rows[ultraDataSource1.Rows.Count - 1]["WName"] = split[1]; ultraDataSource1.Rows[ultraDataSource1.Rows.Count - 1]["SerialNumber"] = split[0]; ultraDataSource1.Rows[ultraDataSource1.Rows.Count - 1]["Market"] = isTwse ? "TWSE" : "OTC"; } return(true); } catch (WebException) { if (isTwse) { MessageBox.Show("可能要更新Key,或是網頁有問題"); } return(false); } catch (NullReferenceException) { if (isTwse) { MessageBox.Show("TWSE 沒有資料"); } else { MessageBox.Show("OTC 沒有資料"); } return(false); } catch (Exception e) { MessageBox.Show(e.ToString()); // return(false); } }
private async Task <bool> ParseHtml(string url, bool isTwse) { try { string firstResponse = await GlobalUtility.GetHtmlAsync(url, System.Text.Encoding.Default);//EDLib.Utility.GetHtml(url, System.Text.Encoding.Default); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(firstResponse); HtmlNodeCollection navNodeChild = doc.DocumentNode.SelectSingleNode("//table[1]").ChildNodes; // /td[1]/table[1]/tr[2] int loopend = navNodeChild.Count; for (int i = 5; i < loopend; i += 2) { //MessageBox.Show(navNodeChild[i].InnerText); string[] split = navNodeChild[i].InnerText.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); //" ", "\t", " ", if (split.Length != 12) { continue; } DataRow dr = dt.NewRow(); dr["Issuer"] = split[0]; dr["WarrantName"] = split[1]; dr["UnderlyingID"] = split[2]; dr["IssueNum"] = split[3]; dr["exeRatio"] = split[4]; dr["ApplyTime"] = split[5]; if (double.TryParse(split[6], out double result)) { dr["AvailableShares"] = split[6]; dr["LastDayUsedShares"] = split[7]; dr["TodayApplyShares"] = split[8]; dr["AccUsedShares"] = split[9]; } else { dr["AvailableShares"] = 0; dr["LastDayUsedShares"] = 0; dr["TodayApplyShares"] = 0; } if (!split[10].StartsWith(" ")) { dr["SameUnderlying"] = split[10]; } if (!split[11].StartsWith(" ")) { dr["OriApplyTime"] = split[11]; } dt.Rows.Add(dr); } return(true); } catch (WebException) { if (isTwse) { MessageBox.Show("可能要更新Key,或是網頁有問題"); } return(false); } catch (NullReferenceException) { if (isTwse) { MessageBox.Show("TWSE 沒有資料"); } else { MessageBox.Show("OTC 沒有資料"); } return(false); } catch (Exception e) { MessageBox.Show(e.ToString()); return(false); } }