crawling_time.js 2.59 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
module.exports = {
    search_broadcasting_time : function(driver,broadcast,day,findtitle, callback) {
        let url1 = "https://search.naver.com/search.naver?sm=top_hty&fbm=0&ie=utf8&query="+broadcast+"+"+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')
                                    str(function() {
                                        i++
                                        if(i==2) {
                                            fs.writeFileSync("./log/"+findtitle+"_time_detail.txt", '\ufeff' + result_arr, {encoding: 'utf8'});
                                            callback()
                                        }
                                    })
                                    function str(_callback) {
                                        temp[i+0] = arr[0] + " " + arr[1].substr(0,2) + "분"
                                        temp[i+1] = arr[1].substr(3)
                                        result_arr.push(temp[i+0],temp[i+1])
                                        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);

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