crawling_samename.js 3.37 KB
var async = require('async');
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome')
var Options = new chrome.Options();
Options.addArguments('headless')
Options.addArguments('disable-gpu')
const fs = require('fs')
var check = 0
var testStartRunTime = 0;
var testruntime = 0;

const {Builder, By, Key, until} = require('selenium-webdriver');

var next_link_list = []
var src_list = []
var text_list = []
var return_list = []

module.exports = {
    first_search :function (driver,string,callback) {
          
        function testRunTimer() {
            var today = new Date(); // 현재시간 얻기
            var runTime = today.getTime(); // 밀리세컨드 ( 1970/01/01 부터 현재까지의 시간을 밀리세컨드로 나타냄 )
            var rtn = 0;
        
            if (testStartRunTime == 0) {
                      testStartRunTime = runTime;
            } else {
                      rtn = (runTime - testStartRunTime) / 1000;
                      testStartRunTime = 0;
            }
            return rtn;
        } 
        let url1 = "https://people.search.naver.com/search.naver?sm=tab_hty&where=nexearch&query="+string+"&ie=utf8&x=0&y=0"
        driver
        .get(url1).then(() => {
            testRunTimer()
            driver
            .findElements(webdriver.By.className('result_profile'))
            .then(profiles => {
                profiles.forEach(profile => {
                    profile.findElements(webdriver.By.className('thmb'))
                    .then(links=> {
                        links.forEach(link => {
                            link.getAttribute('href')
                            .then(next_link => {
                                profile.findElements(webdriver.By.className('thmb_img'))
                                .then((imgs) => {
                                    imgs.forEach(img => {
                                        img.getAttribute('src')
                                        .then(src => {
                                            next_link_list.push(next_link)
                                            src_list.push(src)
                                            //text_list.push(text)
                                            testruntime += testRunTimer()
                                            setTimeout(function() {
                                                check++;
                                                if(check==1) {
                                                    //return_list.push(text_list)
                                                    setTimeout(function() {
                                                        fs.writeFileSync("./log/"+string+"_next_link.txt", '\ufeff' + next_link_list, {encoding: 'utf8'});
                                                        fs.writeFileSync("./log/"+string+"_img_src.txt", '\ufeff' + src_list, {encoding: 'utf8'});
                                                        callback()
                                                    },500)
                                                }
                                            },testruntime*1000+1000)
                                        })
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })
    }
}