예제 #1
0
        public GridResultData SubGridDataSet(string KeyWord, bool tagYn)
        {
            GridResultData Result = new GridResultData();

            //List<KeyWordResult> AllResult = new List<KeyWordResult>();
            //List<KeyWordResult> RefResult = new List<KeyWordResult>();
            List <KeywordList> RefShopKeyWord = new List <KeywordList>();
            ///List<ShopAPIResult> OpenApiList = new List<ShopAPIResult>();
            ///List<String> TitleKeywordList = new List<string>();

            //OpenApiDataSetResult openApiDataSetResult = new OpenApiDataSetResult();

            NaverApi             naverApi        = new NaverApi();
            NaverShoppingCrawler shoppingCrawler = new NaverShoppingCrawler();

            // 쇼핑 연관검색어와 SEO 태그를 조회한다.
            ReturnToMessage("쇼핑연관 검색어를 조회중입니다.");

            // 네이버 쇼핑 연관 검색어 + 태그 정보
            RefShopKeyWord = naverApi.ShopRelKeyword(KeyWord, out int TotalProdutCount);
            ShopWebResult webResult = new ShopWebResult();

            if (tagYn == true)
            {
                ReturnToMessage("태그정보를 분석합니다.");
                webResult = shoppingCrawler.SmartStoreInfoFinder(KeyWord);
            }
            else
            {
                ReturnToMessage("SEO 태그 분석을 제외하였습니다.");
            }

            Console.WriteLine("전 : " + RefShopKeyWord.Count);

            Result = new GridResultData()
            {
                ShoppingRefGrid = RefShopKeyWord, ShopWebDataResult = webResult
            };

            return(Result);
        }
예제 #2
0
        /// <summary>
        /// 스마트 스토어 정보 크롤링
        /// </summary>
        /// <param name="relKeyword"></param>
        /// <returns></returns>
        public ShopWebResult SmartStoreInfoFinder(string relKeyword)
        {
            /**
             * ### 엑셀에 시트별로 분석 데이터 출력 ###
             *  - 전체상품 갯수
             *  - 1page에 있는 광고를 제외한 중복제거된 스마트스토어 태그
             *  - 상품명 리스트 나열
             *  - 상품명 또는 키워드 리스트화 중복제거
             *  - 많이 쓰는 카테고리 정보
             *  - 중복제거된 쇼핑몰명
             *  -
             **/

            List <KeywordList> tagList       = new List <KeywordList>();
            List <string>      mallList      = new List <string>();
            List <string>      productNmList = new List <string>();
            List <Dictionary <string, string> > resultDataList = new List <Dictionary <string, string> >();

            string url = "https://search.shopping.naver.com/search/all.nhn?origQuery=" + relKeyword + "&pagingIndex=" + Convert.ToString(1) + "&pagingSize=80&viewType=list&sort=rel&frm=NVSHPAG&query=" + relKeyword;

            string             textHtml           = HttpWebRequestText(url);
            int                totalCount         = totalProdutCount(textHtml);
            List <KeywordList> shoppingRefKeyWord = ShoppingKeywordHtml(textHtml);


            resultDataList = JSONParser(textHtml, 1, relKeyword, out int adCount);

            int rowidx = 1;

            if (totalCount > 0)
            {
                foreach (Dictionary <string, string> resultDic in resultDataList)
                {
                    string count        = resultDic["count"];
                    string naverArea    = resultDic["naverArea"];
                    string classInfo    = resultDic["classInfo"];
                    string productUrl   = resultDic["productUrl"];
                    string productPrice = resultDic["productPrice"];
                    string productName  = resultDic["productName"];
                    string mallName     = resultDic["mallName"];
                    string categoryName = resultDic["categoryName"];
                    string pageNo       = resultDic["pageNo"];
                    string Keyword      = resultDic["Keyword"];
                    string smartFarmYn  = resultDic["smartFarmYn"];
                    string categoryText = resultDic["categoryText"];

                    productNmList.Add(productName);
                    ReturnToLabel(productName);
                    if (smartFarmYn == "true")
                    {
                        if (classInfo.IndexOf("ad") < 0)
                        {
                            if (rowidx < 20)
                            {
                                string key   = string.Empty;
                                string value = string.Empty;

                                Dictionary <string, string> dicData = new Dictionary <string, string>();

                                // 리다이렉트 상품으로 이동
                                Thread.Sleep(1000);
                                string           reouttext = HttpWebRequestText(productUrl);
                                agi.HtmlDocument redoc     = new agi.HtmlDocument();
                                redoc.LoadHtml(reouttext);

                                IList <agi.HtmlNode> nodes = redoc.DocumentNode.SelectNodes("//*[@class='tb_view2']");

                                if (nodes != null)
                                {
                                    if (nodes.Count > 0)
                                    {
                                        int nodeCnt = 0;
                                        foreach (var node in nodes)
                                        {
                                            if (nodeCnt == 0)
                                            {
                                                IList <agi.HtmlNode> thNodes = nodes.QuerySelectorAll("tbody > tr > th");
                                                IList <agi.HtmlNode> tdNodes = nodes.QuerySelectorAll("tbody > tr > td");
                                                int node2Cnt = 0;
                                                foreach (var node2 in thNodes)
                                                {
                                                    key          = node2.InnerText;
                                                    value        = tdNodes[node2Cnt].InnerText;
                                                    dicData[key] = value;
                                                    node2Cnt++;
                                                }
                                            }
                                        }
                                    }
                                }
                                agi.HtmlNode tagNode = tagNode = redoc.DocumentNode.SelectSingleNode("//*[@name='keywords']");



                                string tagData = string.Empty;



                                if (tagNode != null)
                                {
                                    tagData = tagNode.Attributes["content"].Value;
                                    List <string> tagDataList = new List <string>(tagData.Split(','));
                                    foreach (var data in tagDataList)
                                    {
                                        ReturnToLabel(data);
                                        if (data.Length > 0)
                                        {
                                            tagList.Add(new KeywordList()
                                            {
                                                Keyword = data, Kind = "T"
                                            });
                                        }
                                    }

                                    Console.WriteLine(tagData.Trim());
                                }
                                ReturnToLabel(mallName);
                                mallList.Add(mallName);
                                rowidx++;
                            }
                        }
                    }
                }
            }

            //중복 제거
            tagList  = tagList.Distinct().ToList();  //SEO 태그리스트
            mallList = mallList.Distinct().ToList(); //몰 태그리스트
            // 결과전송
            ShopWebResult result = new ShopWebResult()
            {
                RelKeyword         = relKeyword,         // 키워드
                AdCount            = adCount,            // 첫페이지 광고수
                TotalCount         = totalCount,         // 네이버 쇼핑 상품수
                OutTagList         = tagList,            // SEO 태그 리스트
                ShoppingRefKeyWord = shoppingRefKeyWord, //쇼핑 연관검색어 리스트
                MallList           = mallList,           // 상위 스마트 스토어명 리스트
                ProductNmList      = productNmList       // 상위 상품명 리스트
            };

            return(result);
        }