Example #1
0
        //Data 수집 버튼
        //Naver 주식 정보 크롤링
        //https://html-agility-pack.net/
        ////https://finance.naver.com/item/sise_day.nhn?code=084680 (일별 시세)
        //https://finance.naver.com/item/sise_day.nhn?code=084680&page=1
        //jusik_code = "084680";
        //https://finance.naver.com/item/sise_time.nhn?code=084680&thistime=20200224161036
        private void button4_Click(object sender, EventArgs e)
        {
            AI_Learn_flg = true;
            html_addr HTML_ADDR = new html_addr();

            if (BAP_cliked == true)
            {
                //NEW
                //Page1 10days
                textBox8.Text = HTML_ADDR.html_HtmlDoc_page1(jusik_code, ref stock);
                //Page2 20days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page2(jusik_code, ref stock);
                //Page3 30days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page3(jusik_code, ref stock);
                //Page4 40days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page4(jusik_code, ref stock);
                //Page5 50days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page5(jusik_code, ref stock);
                //Page6 60days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page6(jusik_code, ref stock);
#if false
                //시가,고가,저가,거래량 --> 종가   총 5개 데이터 필요, 5일선,20일선,60일선
                var html = @"https://finance.naver.com/item/sise_day.nhn?code=";
                var test = jusik_code + "&page=1";
                html += test; // 주식 정보 종합

                HtmlWeb web     = new HtmlWeb();
                var     HtmlDoc = web.Load(html);

                int      carry    = 0;
                string[] s_string = new string[_days];

                var htmlNodes_1 = HtmlDoc.DocumentNode.SelectNodes("//body/table[1]/tr[3]");
                var htmlNodes_2 = HtmlDoc.DocumentNode.SelectNodes("//body/table[1]/tr[4]");
                var htmlNodes_3 = HtmlDoc.DocumentNode.SelectNodes("//body/table[1]/tr[5]");
                var htmlNodes_4 = HtmlDoc.DocumentNode.SelectNodes("//body/table[1]/tr[6]");
                var htmlNodes_5 = HtmlDoc.DocumentNode.SelectNodes("//body/table[1]/tr[7]");


                if (htmlNodes_1 == null)
                {
                    err_cnt++; textBox8.Text = "error " + err_cnt + "\n"; return;
                }
                if (htmlNodes_2 == null)
                {
                    err_cnt++; textBox8.Text = "error " + err_cnt + "\n"; return;
                }
                if (htmlNodes_3 == null)
                {
                    err_cnt++; textBox8.Text = "error " + err_cnt + "\n"; return;
                }
                if (htmlNodes_4 == null)
                {
                    err_cnt++; textBox8.Text = "error " + err_cnt + "\n"; return;
                }
                if (htmlNodes_5 == null)
                {
                    err_cnt++; textBox8.Text = "error " + err_cnt + "\n"; return;
                }

                //td1 날짜, td2 종가, td3 전일비, td4 시가, td5 고가, td6 저가 td7 거래량
                foreach (var node in htmlNodes_1)
                {
                    if (node != null)
                    {
                        var data_date               = node.SelectSingleNode("td[1]").InnerText;
                        var data_closing_price      = node.SelectSingleNode("td[2]").InnerText;
                        var data_market_price       = node.SelectSingleNode("td[4]").InnerText;
                        var data_high_price         = node.SelectSingleNode("td[5]").InnerText;
                        var data_low_price          = node.SelectSingleNode("td[6]").InnerText;
                        var data_transaction_volume = node.SelectSingleNode("td[7]").InnerText;
                        textBox8.Text = "Date:" + data_date + " 종가:" + data_closing_price + " 시가:" + data_market_price +
                                        " 고가:" + data_high_price + " 저가:" + data_low_price + " 거래량:" + data_transaction_volume + Environment.NewLine;

                        s_date[carry]    = call_method.CnvStringToInt_4(data_date);
                        s_dcp_int[carry] = call_method.CnvStringToInt(data_closing_price);
                        s_dtv_int[carry] = call_method.CnvStringToInt(data_transaction_volume);
                        s_dmp_int[carry] = call_method.CnvStringToInt(data_market_price);
                        s_dhp_int[carry] = call_method.CnvStringToInt(data_high_price);
                        s_dlp_int[carry] = call_method.CnvStringToInt(data_low_price);
                        carry++;
                    }
                }

                foreach (var node in htmlNodes_2)
                {
                    if (node != null)
                    {
                        var data_date               = node.SelectSingleNode("td[1]").InnerText;
                        var data_closing_price      = node.SelectSingleNode("td[2]").InnerText;
                        var data_market_price       = node.SelectSingleNode("td[4]").InnerText;
                        var data_high_price         = node.SelectSingleNode("td[5]").InnerText;
                        var data_low_price          = node.SelectSingleNode("td[6]").InnerText;
                        var data_transaction_volume = node.SelectSingleNode("td[7]").InnerText;
                        textBox8.Text += "Date:" + data_date + " 종가:" + data_closing_price + " 시가:" + data_market_price +
                                         " 고가:" + data_high_price + " 저가:" + data_low_price + " 거래량:" + data_transaction_volume + Environment.NewLine;

                        s_date[carry]    = call_method.CnvStringToInt_4(data_date);
                        s_dcp_int[carry] = call_method.CnvStringToInt(data_closing_price);
                        s_dtv_int[carry] = call_method.CnvStringToInt(data_transaction_volume);
                        s_dmp_int[carry] = call_method.CnvStringToInt(data_market_price);
                        s_dhp_int[carry] = call_method.CnvStringToInt(data_high_price);
                        s_dlp_int[carry] = call_method.CnvStringToInt(data_low_price);
                        carry++;
                    }
                }

                foreach (var node in htmlNodes_3)
                {
                    if (node != null)
                    {
                        var data_date               = node.SelectSingleNode("td[1]").InnerText;
                        var data_closing_price      = node.SelectSingleNode("td[2]").InnerText;
                        var data_market_price       = node.SelectSingleNode("td[4]").InnerText;
                        var data_high_price         = node.SelectSingleNode("td[5]").InnerText;
                        var data_low_price          = node.SelectSingleNode("td[6]").InnerText;
                        var data_transaction_volume = node.SelectSingleNode("td[7]").InnerText;
                        textBox8.Text += "Date:" + data_date + " 종가:" + data_closing_price + " 시가:" + data_market_price +
                                         " 고가:" + data_high_price + " 저가:" + data_low_price + " 거래량:" + data_transaction_volume + Environment.NewLine;

                        s_date[carry]    = call_method.CnvStringToInt_4(data_date);
                        s_dcp_int[carry] = call_method.CnvStringToInt(data_closing_price);
                        s_dtv_int[carry] = call_method.CnvStringToInt(data_transaction_volume);
                        s_dmp_int[carry] = call_method.CnvStringToInt(data_market_price);
                        s_dhp_int[carry] = call_method.CnvStringToInt(data_high_price);
                        s_dlp_int[carry] = call_method.CnvStringToInt(data_low_price);
                        carry++;
                    }
                }

                foreach (var node in htmlNodes_4)
                {
                    if (node != null)
                    {
                        var data_date               = node.SelectSingleNode("td[1]").InnerText;
                        var data_closing_price      = node.SelectSingleNode("td[2]").InnerText;
                        var data_market_price       = node.SelectSingleNode("td[4]").InnerText;
                        var data_high_price         = node.SelectSingleNode("td[5]").InnerText;
                        var data_low_price          = node.SelectSingleNode("td[6]").InnerText;
                        var data_transaction_volume = node.SelectSingleNode("td[7]").InnerText;
                        textBox8.Text += "Date:" + data_date + " 종가:" + data_closing_price + " 시가:" + data_market_price +
                                         " 고가:" + data_high_price + " 저가:" + data_low_price + " 거래량:" + data_transaction_volume + Environment.NewLine;

                        s_date[carry]    = call_method.CnvStringToInt_4(data_date);
                        s_dcp_int[carry] = call_method.CnvStringToInt(data_closing_price);
                        s_dtv_int[carry] = call_method.CnvStringToInt(data_transaction_volume);
                        s_dmp_int[carry] = call_method.CnvStringToInt(data_market_price);
                        s_dhp_int[carry] = call_method.CnvStringToInt(data_high_price);
                        s_dlp_int[carry] = call_method.CnvStringToInt(data_low_price);
                        carry++;
                    }
                }

                foreach (var node in htmlNodes_5)
                {
                    if (node != null)
                    {
                        var data_date               = node.SelectSingleNode("td[1]").InnerText;
                        var data_closing_price      = node.SelectSingleNode("td[2]").InnerText;
                        var data_market_price       = node.SelectSingleNode("td[4]").InnerText;
                        var data_high_price         = node.SelectSingleNode("td[5]").InnerText;
                        var data_low_price          = node.SelectSingleNode("td[6]").InnerText;
                        var data_transaction_volume = node.SelectSingleNode("td[7]").InnerText;
                        textBox8.Text += "Date:" + data_date + " 종가:" + data_closing_price + " 시가:" + data_market_price +
                                         " 고가:" + data_high_price + " 저가:" + data_low_price + " 거래량:" + data_transaction_volume + Environment.NewLine;

                        s_date[carry]    = call_method.CnvStringToInt_4(data_date);
                        s_dcp_int[carry] = call_method.CnvStringToInt(data_closing_price);
                        s_dtv_int[carry] = call_method.CnvStringToInt(data_transaction_volume);
                        s_dmp_int[carry] = call_method.CnvStringToInt(data_market_price);
                        s_dhp_int[carry] = call_method.CnvStringToInt(data_high_price);
                        s_dlp_int[carry] = call_method.CnvStringToInt(data_low_price);
                        carry++;
                    }
                }

                //chart add
                chart1.Series["Series1"].Points.Clear();
                chart1.Series["Series1"].Points.AddXY(s_date[4].ToString("D4"), s_dcp_int[4]);
                chart1.Series["Series1"].Points.AddXY(s_date[3].ToString("D4"), s_dcp_int[3]);
                chart1.Series["Series1"].Points.AddXY(s_date[2].ToString("D4"), s_dcp_int[2]);
                chart1.Series["Series1"].Points.AddXY(s_date[1].ToString("D4"), s_dcp_int[1]);
                chart1.Series["Series1"].Points.AddXY(s_date[0].ToString("D4"), s_dcp_int[0]);
#endif

                //chart add
                chart1.Series["Series1"].Points.Clear();
                for (int i = GG._days - 1; i > 0; i--)
                {
                    chart1.Series["Series1"].Points.AddXY(stock[i].s_date.ToString("D4"), stock[i].s_dcp_int);
                }
            }
            else if (smart_cliked == true)
            {
                //NEW
                //Page1 10days
                textBox8.Text = HTML_ADDR.html_HtmlDoc_page1_v2(jusik_code, ref stk_v2);
                //Page2 20days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page2_v2(jusik_code, ref stk_v2);
                //Page3 30days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page3_v2(jusik_code, ref stk_v2);
                //Page4 40days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page4_v2(jusik_code, ref stk_v2);
                //Page5 50days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page5_v2(jusik_code, ref stk_v2);
                //Page6 60days
                textBox8.Text += HTML_ADDR.html_HtmlDoc_page6_v2(jusik_code, ref stk_v2);

                //chart add
                chart1.Series["Series1"].Points.Clear();
                for (int i = GG._days - 1; i > 0; i--)
                {
                    chart1.Series["Series1"].Points.AddXY(stk_v2[i].s_date.ToString("D4"), stk_v2[i].s_dcp_int);
                }
            }
            else
            {
                textBox8.Text = "SELECT ERR";
            }
        }
        public string html_HtmlDoc_page6_v2(string jusik_code, ref stock_v2[] stock_60days)
        {
            //Initial
            Global_days GG = new Global_days();

            stock_v2[] stock_page6 = new stock_v2[GG._days];
            string     put         = "";

            stock_page6 = stock_60days; //ref

            //Method Set
            MethodClass call_method = new MethodClass();

            var html = @"https://finance.naver.com/item/sise_day.nhn?code=";
            var test = jusik_code + "&page=6";

            html += test; // 주식 정보 종합

            HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
            var HtmlDoc = web.Load(html);

            //html_addr html_Addr = new html_addr();
            //html_Addr.html_HtmlDoc(HtmlDoc);

            int carry = 50;

            string[] s_string = new string[GG._days];

            HtmlAgilityPack.HtmlNodeCollection[] htmlNodes = new HtmlAgilityPack.HtmlNodeCollection[GG.divide_days];

            //3,4,5,6,7
            //11,12,13,14,15
            for (int i = 0; i < GG.divide_days; i++)
            {
                int jump = 3;
                if (i >= 5)
                {
                    // ex) i=5 + jump -> 11
                    jump = 6;
                }
                jump        += i;
                htmlNodes[i] = HtmlDoc.DocumentNode.SelectNodes("//body/table[1]/tr[" + jump + "]");
                if (htmlNodes[i] == null)
                {
                    return(i + jump + "err");
                }

                //td1 날짜, td2 종가, td3 전일비, td4 시가, td5 고가, td6 저가 td7 거래량
                foreach (var node in htmlNodes[i])
                {
                    if (node != null)
                    {
                        var data_date               = node.SelectSingleNode("td[1]").InnerText;
                        var data_closing_price      = node.SelectSingleNode("td[2]").InnerText;
                        var data_market_price       = node.SelectSingleNode("td[4]").InnerText;
                        var data_high_price         = node.SelectSingleNode("td[5]").InnerText;
                        var data_low_price          = node.SelectSingleNode("td[6]").InnerText;
                        var data_transaction_volume = node.SelectSingleNode("td[7]").InnerText;

                        put += "Date:" + data_date + " 종가:" + data_closing_price + " 시가:" + data_market_price +
                               " 고가:" + data_high_price + " 저가:" + data_low_price + " 거래량:" + data_transaction_volume + Environment.NewLine;

                        //stock[carry].s_date = call_method.CnvStringToInt_4(data_date);
                        stock_page6[carry].s_date    = call_method.CnvStringToInt_4(data_date);
                        stock_page6[carry].s_dcp_int = call_method.CnvStringToInt(data_closing_price);
                        stock_page6[carry].s_dmp_int = call_method.CnvStringToInt(data_market_price);
                        stock_page6[carry].s_dhp_int = call_method.CnvStringToInt(data_high_price);
                        stock_page6[carry].s_dlp_int = call_method.CnvStringToInt(data_low_price);
                        carry++;
                    }
                }
            }
            return(put);
        }