crawling_samename.js 7.06 KB
var async = require('async');
var webdriver = require('selenium-webdriver');
var options = {desiredCapabilities: {browserName: 'chrome'}};
var JASON = require('jason')
const {Builder, By, Key, until} = require('selenium-webdriver');



var ajason = new Object()
var aname = []
var asrc=[]
var alink=[]
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
let url1 = "https://people.search.naver.com/search.naver?sm=tab_hty&where=nexearch&query=%EC%9C%A0%EC%9E%AC%EC%84%9D&ie=utf8&x=0&y=0"
var driver_1 = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();

driver
.get(url1).then(() => {
    driver
    .findElements(webdriver.By.className('result_profile'))
    .then(whoclasses=> {
        whoclasses.forEach(whoclass => {
                whoclass.findElements(webdriver.By.className('thmb'))
                .then(img_thmbs => {
                    img_thmbs.forEach(img_thmb => {
                        img_thmb.findElements(webdriver.By.className('thmb_img')).then(img_src => {
                            
                            img_src[0].getAttribute("src")
                            .then(src => {
                                asrc.push(src)
                                console.log(src)
                            }).then(() => {
                                console.log(asrc)
                            })
                        })
                    })
                    whoclass.findElements(webdriver.By.tagName("strong"))
                    .then(name_temp => {
                        name_temp[0].getText().then(name => {
                            aname.push(name)
                        })
                    })
            })
            whoclass.findElements(webdriver.By.className('who'))
            .then(who => {
                who[0].findElements(webdriver.By.className('name'))
                .then(findname => {
                    findname[0].getAttribute("href")
                    .then(link => {
                        alink.push(link)
                    }).then(() => {
                        driver_1.get(alink[0]).then(() => {
                            driver_1
                            .findElement(webdriver.By.id('pagination_76'))
                            .then(paginationBtn => {
                                paginationBtn.findElements(webdriver.By.className('bt_next'))
                                .then(elemsBtn => {
                                    var cnt = 1;
                                    
                                    function getContentsAndClickNext (callback) {
                                        console.log('higetcontests', cnt);
                                        cnt++;
                                        driver_1
                                            .findElement(webdriver.By.id('listUI_76'))
                                            .then(contentsUI => {
                                                contentsUI
                                                    .findElements(webdriver.By.tagName('li'))
                                                    .then(elems => {
                                                        elems.forEach(elem => {
                                                            elem
                                                                .getText()
                                                                .then(text => { 
                                                                    console.log(text);
                                                                    // 내 추측 : stale해지면, boolean 값으로 driver 어딘가에 true설정되는것 같아
                                                                    // wait이라는 함수를 써서, 이게 false 가 될 때까지 기다린 다음에 getText()를 해.
                                                                    // ok?
                                                                })
                                                                .catch(error => {
                                                                    // 문제가 있으면 이곳을 의심해볼것! 변수 error를 체크해보기!
                                                                    // console.log('really?? exception!');
                                                                })
                                                        })
                                                        
                                                        elemsBtn[0].click();
                
                                                        sleep(50).then(() => {
                                                            // Do something after the sleep!
                                                        });
                                                        callback();
                                                    })
                                                    .catch(error => {
                                                        console.log("asdfasdf!!', e");
                                                    })
                                            })
                                            .catch(error => {
                                                console.log('fuck!@#', error);
                                            })
                                    }
                                
                                    async.whilst(
                                        function() {
                                            console.log('whilist result', cnt < 5);
                                            return cnt < 5;
                                        },
                                        getContentsAndClickNext,
                                        function(e) {
                                            console.log('Exception', e)
                                        }
                                    );
                                })
                                .catch(error => {
                                    console.log('Exception 4444', error);
                                }); // 다음 컨텐츠로 가는 버튼(2개임. 하나는 평소용 두번째는 더이상 갈 페이지가 없을 때 쓰는 버튼)
                            })
                        .catch(error => {
                            console.log('Exception 555', error);
                        }); // 방송 컨텐츠 페이지네이션 버튼
                    
                    
                    // sleep time expects milliseconds
                        function sleep (time) {
                            return new Promise((resolve) => setTimeout(resolve, time));
                        }
                    
                          // Usage!
                         sleep(8000).then(() => {
                            // Do something after the sleep!
                        });
                    
                    });
                    })
                })
            })
        })

    })
})