Example #1
0
        getArticleInfoAsync(int MAX_SIZE, string URL, string TITLE, string IMG, string numSelector)
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                /*go to url*/
                await page.GoToAsync(URL);

                /*get random num(1~MAX_SIZE)*/
                var random    = new Random();
                var randomNum = random.Next(1, MAX_SIZE);

                /*get element from selector(title, img)*/
                var titleHandle = await selectArticleAsync(
                    articleNumber(numSelector, randomNum), TITLE, page);

                /*get article information from element(text, imgSrc, url)*/
                var title = titleHandle.GetPropertyAsync("text").Result.ToString().Replace("JSHandle:", "");
                var href  = titleHandle.GetPropertyAsync("href").Result.ToString().Replace("JSHandle:", "");

                ElementHandle imgHandle;
                await Task.Delay(3000);

                await titleHandle.ClickAsync();

                await Task.Delay(3000);

                try
                {
                    imgHandle = await page.WaitForSelectorAsync(IMG, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch
                {
                    imgHandle = await page.WaitForSelectorAsync("#newsEndContents > span:nth-child(1) > img", new WaitForSelectorOptions { Timeout = 3000 });
                }
                var imgSrc = imgHandle.GetPropertyAsync("src").Result.ToString().Replace("JSHandle:", "");

                await browser.CloseAsync();

                browser.Dispose();
                return(await ArticleFactory.makeArticleAsync(title, href, imgSrc));
            }
        }