是否可以使用Break跳出或跳过爬网功能?

时间:2018-06-25 23:42:05

标签: javascript node.js database web-scraping web-crawler

嗨,我想知道是否有可能在首次运行脚本后使用“ break”或任何其他方法跳过爬网函数,而只是使用包含用于下一个用户请求的爬网信息的数组变量

let glossary = [];  

/** Initialise crawling and save it into glossary array */
function init() {
    const glossary_url = 'https://xxxx';

     const headers = {
         cookie: 'cookies:kdkjslkd424'  
     };

    const options = {
        url: glossary_url,
        method: 'GET',
        headers: headers
    };

    request(options, function (error, response, body) {
        const newDom = new jsdom(body);
        const $ = require('jquery')(newDom.window);

        $('ul > li > span[style="line-height:1.6;"]').each(function (index, element) {
            let text = element.textContent.trim(); // Get text from html
            let splitText = text.split(' = '); // split text by =
            //console.log(text);


            if (splitText.length > 1) {
                glossary.push({
                    key: splitText[0].trim(),
                    value: splitText[1],
                    value2: splitText[2]
                });
            }
        });
        //console.log(glossary);

        findMatch('DPDL');
    });
}
 break init;

function findMatch (key){
    for(i = 0; i < glossary.length ; i++) {
        if (glossary[i].key === key){
            // console.log (glossary[i].value );
            // console.log(glossary[i].key);
            // console.log(glossary[i].value2);
            // console.log(key);
            console.log(key + ' = ' + glossary[i].value + ' ' + glossary[i].value2 );
        } 
    } 

}


init();  

如果用户要搜索仅在词汇表数组 glossary = [] 中找到的另一个值,则不进行爬网或跳过爬网功能,因为需要很长时间才不再进行爬网

0 个答案:

没有答案