crawling_time.js 3.21 KB
var webdriver = require('selenium-webdriver');
const {Builder, By, Key, until} = require('selenium-webdriver');
const fs = require('fs')

    var arr = []
    var result_arr = []
    var temp = []
    var i=0
    var check = 0

    
//var driver = new webdriver.Builder().forBrowser('chrome').withCapabilities(webdriver.Capabilities.chrome()).build();
//search_broadcasting_time(driver,"SBS","일요일","런닝맨",function() {})
module.exports = {
    search_broadcasting_time :function (driver,broadcast,day,findtitle, callback) {
        let url1 = "https://search.naver.com/search.naver?sm=top_hty&fbm=1&ie=utf8&query="+broadcast.toLowerCase()+"+"+day+"요일"+"+편성표"
        driver
        .get(url1).then(() => {
            driver
            .findElements(webdriver.By.className('cont_inner type_day _scheduleArea'))
            .then(contentsearch_section=> {
                contentsearch_section[0].findElements(webdriver.By.tagName('tbody'))
                .then(tbody => {
                    tbody[0].findElements(webdriver.By.tagName('tr'))
                    .then(trs => {
                        trs.forEach(tr => {
                            tr.getText()
                            .then(content => {
                                if(content.indexOf(findtitle) != -1) {
                                    arr = content.toString().split('\n')
                                    console.log(arr)
                                    str(function() {
                                        i++
                                        if(i==1) {
                                            R=[]
                                            R[0] = result_arr[0]
                                            R[1] = result_arr[1]
                                            R[2] = result_arr[2]
                                            fs.writeFileSync("./log/"+findtitle+"_time_detail.txt", '\ufeff' + R, {encoding: 'utf8'});
                                            callback()
                                        }
                                    })
                                    function str(_callback) {
                                        temp[0] = arr[0].substr(0,2)
                                        temp[1] = arr[1].substr(0,2)
                                        temp[2] = findtitle
                                        result_arr.push(temp[0],temp[1],temp[2])
                                        //if(arr.length == 3) {
                                        //    temp[i+2] = arr[0] + " " + arr[2].substr(0,2) + "분"
                                        //    temp[i+3] = arr[2].substr(3)
                                        //    result_arr.push(temp[i+2],temp[i+3])
                                        //    i++
                                        //}
                                        setTimeout(() => {
                                            _callback()
                                        }, 500);

                                    } 
                                }
                            })
                        })
                    })
                })
            })
        })
    }
}