Beispiel #1
0
 public bool IsExist(StockDividend stockDividend)
 {
     return(_conn.ExecuteScalar <bool>(
                "select count(1) from StockDividend where stock_id=@stock_id and time_string=@time_string",
                stockDividend
                ));
 }
        public IEnumerable <StockDividend> ParseHtml(string html, string stock_id)
        {
            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(html);
            var tableNodes = doc.DocumentNode.SelectNodes("//table[@class=\"hasBorder\"]");

            foreach (var tableNode in tableNodes)
            {
                var trNodes = tableNode.SelectNodes("./tr");
                int trIndex = 0;
                Dictionary <string, int> headers = new Dictionary <string, int>();

                // header
                var thNodes_0   = trNodes[0].SelectNodes("./th");
                var thNodes_1   = trNodes[1].SelectNodes("./th");
                int headerIndex = 0;
                foreach (var thNode_0 in thNodes_0)
                {
                    if (thNode_0.InnerText != "股東配發內容")
                    {
                        headers.Add(thNode_0.InnerText, headerIndex++);
                    }
                    else
                    {
                        foreach (var thNode_1 in thNodes_1)
                        {
                            headers.Add(thNode_1.InnerText.Replace(" ", ""), headerIndex++);
                        }
                    }
                }

                foreach (var trNode in trNodes)
                {
                    //data
                    var tdNodes = trNode.SelectNodes("./td");
                    if (tdNodes != null)
                    {
                        string timeString = tdNodes[headers["股利所屬年(季)度"]].InnerText.Replace("&nbsp;", "");
                        Regex  regex      = new Regex("([0-9]*)年(第([1-4])季|年度)", RegexOptions.Singleline);
                        var    matchs     = regex.Match(timeString);

                        int year;
                        int?season = null;
                        if (matchs.Groups.Count >= 2)
                        {
                            year = Convert.ToInt32(matchs.Groups[1].Value);
                        }
                        else
                        {
                            throw new Exception("Not Find year Data");
                        }
                        if (matchs.Groups.Count >= 4 && matchs.Groups[3].Value != "")
                        {
                            season = Convert.ToInt32(matchs.Groups[3].Value);
                        }

                        var stockDividend = new StockDividend()
                        {
                            stock_id    = stock_id,
                            time_string = timeString,
                            year        = year,
                            season      = season,
                            dividend    = Convert.ToDecimal(tdNodes[headers["盈餘分配之現金股利(元/股)"]].InnerText.Replace("&nbsp;", ""))
                        };
                        yield return(stockDividend);
                    }
                    trIndex++;
                }
            }
        }