예제 #1
0
        /// <summary>
        /// 선택 된 전체 노드 가져오기
        /// </summary>
        /// <param name="pageSource"></param>
        /// <param name="xPathModels"></param>
        /// <returns></returns>
        private IEnumerable <BeautifulNode> GetSelectNodes(string pageSource, List <XPathModel> xPathModels)
        {
            // 설명 : 애매한 상황들 때문에 어쩔 수 없이 모든 xPath로 노드 수집 후 상품코드가 가장 많이 수집 되는 노드 리턴

            IEnumerable <BeautifulNode>         result    = new List <BeautifulNode>();
            List <IEnumerable <BeautifulNode> > resultTmp = new List <IEnumerable <BeautifulNode> >();

            foreach (var xPathModel in xPathModels)
            {
                if (string.IsNullOrEmpty(xPathModel.PageNodeXPath))
                {
                    continue;
                }

                var page  = new BeautifulPage(pageSource);
                var nodes = page.SelectNodes(xPathModel.PageNodeXPath);

                resultTmp.Add(nodes);
            }

            // goodscode 포함 돼 있는 노드들 가져오기
            var resultTmp2 = resultTmp.Select(n => n.Where(nc => nc.Href.ToLower().Contains("goodscode")));

            // goodscode 가 가장 많이 포함된 노드 반환 (애매하게 몇개씩 껴서 수집 되는 경우가 있어 제일 많은 갯수가 수집된 노드를 반환)
            foreach (var node in resultTmp2)
            {
                if (result.Count() < node.Count())
                {
                    result = node;
                }
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// 특이 케이스 페이지 처리 ( 타켓 Url -> http://mg.gmarket.co.kr/Promotion/Plan?lcId=&sid=160045 )
        /// </summary>
        /// <param name="startNum"></param>
        /// <param name="endNum"></param>
        /// <param name="xPathModel"></param>
        /// <param name="logFunc"></param>
        /// <returns></returns>
        List <DataModel> GetGoods_Mobile_Alias_type2_2(int startNum, int endNum, XPathModel xPathModel, Action <string> logFunc)
        {
            var result = new List <DataModel>();

            // 페이지 소스 가져오기
            var getPageSourceResult = webController.GetPageSource();

            if (!getPageSourceResult.ResultFlag)
            {
                logFunc($"페이지소스를 가져오는 중 오류 발생 : {getPageSourceResult.Err}");
                return(result);
            }
            var page = new BeautifulPage(getPageSourceResult.ResultValue);

            // 시작 지점 초기화
            int curNumGroup1 = xPathModel.ParamsStartNum[0];
            int curNumGroup2 = xPathModel.ParamsStartNum[1];

            for (int num = 1; num <= endNum; num++, curNumGroup2++)
            {
                // 우선 값 가져오기
                var           xPath = string.Format(xPathModel.NumNodeXPath, curNumGroup1, curNumGroup2);
                BeautifulNode node  = page.SelectNode(xPath);

                // 그룹 끝났는지 체크 후 다시 수집 : 라인 수정 했는데도 null이면 현재 그룹의 상품코드가 더이상 없다고 보고 그룹 옮기기
                if (node == null || string.IsNullOrEmpty(node.Href))
                {
                    curNumGroup1++;                                 // 그룹 옮기고
                    curNumGroup2 = xPathModel.ParamsStartNum[1];    // 상품 순번 첫번째로 이동

                    // 다시 가져오기
                    xPath = string.Format(xPathModel.NumNodeXPath, curNumGroup1, curNumGroup2);
                    node  = page.SelectNode(xPath);
                }

                // 위에서 파라미터 인덱스 증가 시켜줬는데도 null이면 더이상 상품이 없다고 보고 종료
                // 하지만 null이 아니라면 num부터 endNum 까지 계속 루프 돌아줌
                if (node == null || string.IsNullOrEmpty(node.Href))
                {
                    break;
                }

                // 상품코드 가져올 처음번호 이전 꺼는 전부 버리기
                if (num < startNum)
                {
                    continue;
                }

                // 상품코드 추가
                var goodsCode = GetGoodsCode(node.Href);
                result.Add(new DataModel()
                {
                    GooodsCode = goodsCode
                });
            }

            return(result);
        }
        private IEnumerable <BeautifulNode> GetNodes(string script, string xPath)
        {
            int interval = 3;

            do
            {
                var ttttt0 = ChromeDriver.ExecuteJSReturnHtml(script);
                var page0  = new BeautifulPage(ttttt0.ResultValue);
                var ttt0   = page0.SelectNodes(xPath);

                if (ttt0 != null)
                {
                    return(ttt0);
                }

                interval--;
                Thread.Sleep(300);
            }while (interval > 0);

            return(null);
        }
        private List <BeautifulNode> GetChatNodes(string script, string xPath)
        {
            int interval = 3;

            do
            {
                var page0 = new BeautifulPage(script);

                var ttt0 = page0.SelectNodes(xPath).ToList();
                if (ttt0 != null && ttt0.Count > 0)
                {
                    return(ttt0);
                }

                interval--;

                Thread.Sleep(300);
            }while (interval > 0);

            return(new List <BeautifulNode>());
        }
        public List <BjInfoModel> GetBjInfo(RankBjModel rankBjModel)
        {
            var result = new List <BjInfoModel>();

            var url   = $"http://live.afreecatv.com:8079/app/index.cgi?szBjId={rankBjModel.BjID}";
            var html  = GetUserRankHtml(rankBjModel.BjID, url);
            var page  = new BeautifulPage(html);
            var xPath = "/html/body/div[5]/div[4]/div[1]/div[2]/div[4]/ul/li";
            var nodes = page.SelectNodes(xPath);

            for (int Idx = 0; Idx < nodes.Count(); Idx++)
            {
                var node = nodes?.ToList()?[Idx];
                var name = node.Text?.Split(':')?[0].Trim() ?? string.Empty;
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }

                var pHtmlArray = node.Html?.Trim().Replace(" ", "").Split(new string[] { "<span>", "</span>" }, StringSplitOptions.RemoveEmptyEntries);
                var pCount     = pHtmlArray[1];
                var pUint      = name == "방송시간" ? "시간" : "명";

                //Console.WriteLine(rankBjModel.BjID + " nmae : " + name + "html : " + pHtml + "text : " + pText);

                if (node != null)
                {
                    result.Add(new BjInfoModel()
                    {
                        BjID         = rankBjModel.BjID,
                        Name         = name,
                        Count        = pCount,
                        Unit         = pUint,
                        HistoryDepth = rankBjModel.HistoryDepth,
                    });
                }
            }

            return(result);
        }
예제 #6
0
        /// <summary>
        /// xPath 선택자
        /// </summary>
        /// <param name="xPathModels"></param>
        /// <returns></returns>
        private XPathModel GetXPathModel(List <XPathModel> xPathModels)
        {
            // 설명 : 애매한 상황들 때문에 어쩔 수 없이 모든 xPath로 상품코드 노드 수 수집 후 가장 노드 수 가 많은 xPath 리턴

            if (xPathModels == null || xPathModels.Count <= 0)
            {
                return(null);
            }

            XPathModel result = null;

            var getPageSource = webController.GetPageSource();

            if (!getPageSource.ResultFlag)
            {
                return(result);
            }

            foreach (var xPathModel in xPathModels)
            {
                if (string.IsNullOrEmpty(xPathModel.PageNodeXPath))
                {
                    continue;
                }

                var page  = new BeautifulPage(getPageSource.ResultValue);
                var nodes = page.SelectNodes(xPathModel.PageNodeXPath);

                if (nodes != null && nodes.Count() > 0)
                {
                    xPathModel.SelectTotalNode = nodes.Where(nc => nc.Href.ToLower().Contains("goodscode"))?.Count() ?? 0;
                }
            }

            result = xPathModels.OrderByDescending(n => n.SelectTotalNode)?.FirstOrDefault();

            return(result);
        }
예제 #7
0
        /// <summary>
        /// 페이지의 특정 순서 상품코드 가져오기
        /// </summary>
        /// <param name="logFunc"></param>
        /// <returns></returns>
        private string GetIndexGoodsCodeTableType(int codeNum, XPathModel xPathModel, Action <string> logFunc)
        {
            var getPageSourceResult = webController.GetPageSource();

            if (!getPageSourceResult.ResultFlag)
            {
                logFunc(getPageSourceResult.Err);
                return(string.Empty);
            }

            string result = string.Empty;

            try
            {
                var page = new BeautifulPage(getPageSourceResult.ResultValue);

                BeautifulNode node      = null;
                string        xPathText = string.Empty;

                // PC 웹의 경우 html 구조가 여러가지여서 여기서 상황별 대처가 필요함.
                // (현재는 파라미터가 2, 3 개인 케이스가 한건씩 밖에 없어서 이렇게 처리 했는데 다른 케이스가 발견 되면 수정 필요...)
                switch (xPathModel.ParamCount)
                {
                case 1:
                    xPathText = string.Format(xPathModel.NumNodeXPath, codeNum);
                    node      = page.SelectNode(xPathText);
                    break;

                case 2:
                    if (xPathModel.ParamsStartNum.Count == 2 && xPathModel.ColLength > 0)
                    {
                        int startDepth0 = xPathModel.ParamsStartNum[0];      // 첫번째 파라미터 시작번호
                        int startDepth1 = xPathModel.ParamsStartNum[1];      // 두번째 파라미터 시작번호
                        int colLen      = xPathModel.ColLength;              // 총 갯수

                        // 첫번째 파라미터 위치
                        decimal currentTmp = Math.Ceiling((decimal)codeNum / (decimal)colLen);
                        int     param0     = (int)currentTmp;

                        // 두번째 파라미터 위치
                        int param1Tmp = codeNum % colLen;
                        int param1    = param1Tmp == 0 ? colLen : param1Tmp;

                        xPathText = string.Format(xPathModel.NumNodeXPath, param0, param1);
                    }
                    else if (xPathModel.ParamsStartNum.Count == 2 && xPathModel.ColLength == 0)
                    {
                        int startDepth0 = xPathModel.ParamsStartNum[0];      // 첫번째 파라미터 시작번호
                        int startDepth1 = xPathModel.ParamsStartNum[1];      // 두번째 파라미터 시작번호
                        int colLen      = xPathModel.ColLength;              // 총 갯수

                        // 첫번째 파라미터 위치
                        decimal currentTmp = Math.Ceiling((decimal)codeNum / (decimal)colLen);
                        int     param0     = (int)currentTmp;

                        // 두번째 파라미터 위치
                        int param1Tmp = codeNum % colLen;
                        int param1    = param1Tmp == 0 ? colLen : param1Tmp;

                        xPathText = string.Format(xPathModel.NumNodeXPath, param0, param1);
                    }

                    break;
                }

                // xPath를 가져왔다면 상품코드 가져오기
                if (!string.IsNullOrEmpty(xPathText))
                {
                    node   = page.SelectNode(xPathText);
                    result = GetGoodsCode(node.Href);
                }
            }
            catch (Exception e)
            {
                string err = $"상품코드 가져오는 중 오류가 발생했습니다. [{e.Message}]";
                logFunc(err);
            }

            return(result);
        }