javascript - 如何使用 Phantomjs 设置页面抓取之间的时间间隔

标签 javascript node.js web-scraping phantomjs iteration

目前我用 Phantomjs 编写了一个脚本来抓取多个页面。我的脚本有效,但我不知道如何设置抓取之间的时间间隔。我尝试使用 setInterval 并大约每 5 秒从 arrayList 传递一次项目,但它似乎不起作用。我的剧本总是被打破。这是我的 phantomjs 脚本代码示例:

没有setInterval

var arrayList = ['string1', 'string2', 'string3'....]

arrayList.forEach(function(eachItem) {
    var webAddress = "http://www.example.com/eachItem"    
    phantom.create(function(ph) {
    return ph.createPage(function(page) {

        return page.open(yelpAddress, function(status) {
            console.log("opened site? ", status);


            page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {

                setTimeout(function() {
                    return page.evaluate(function() {

                        //code here for gathering data


                    }, function(result) {
                        return result
                        ph.exit();
                    });

                }, 5000);

            });
        });
    });
});

使用setInterval:

var arrayList = ['string1', 'string2', 'string3'....]
var i = 0
var scrapeInterval = setInterval(function() {
    var webAddress = "http://www.example.com/arrayList[i]"    
    phantom.create(function(ph) {
    return ph.createPage(function(page) {

        return page.open(yelpAddress, function(status) {
            console.log("opened site? ", status);


              page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {

                setTimeout(function() {
                    return page.evaluate(function() {

                           //code here for gathering data


                    }, function(result) {
                           return result
                           ph.exit();
                    });

                }, 5000);

            });
        });
    });
    i++
    if(i > arrayList.length) {
    clearInterval(scrapeInterval);        
}, 5000);

基本上,我想在 arrayList 中发送一大块项目(其中 10-20 个),然后等待 1 - 2 分钟,然后发送下一个项目 block ,而不会压垮网站。或者是否有办法设置时间间隔以每 2-3 秒循环遍历数组中的每个项目。

最佳答案

问题在于 PhantomJS 是异步的,但循环迭代不是。所有迭代(在第一个代码片段中)甚至在加载第一页之前都会执行。您实际上是在生成多个同时运行的此类进程。

您可以使用类似 async 的内容让它按顺序运行:

phantom.create(function(ph) {
    ph.createPage(function(page) {
        var arrayList = ['string1', 'string2', 'string3'....];

        var tasks = arrayList.map(function(eachItem) {
            return function(callback){
                var webAddress = "http://www.example.com/" + eachItem;
                page.open(webAddress, function(status) {
                    console.log("opened site? ", status);

                    page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {

                        setTimeout(function() {
                            return page.evaluate(function() {
                                //code here for gathering data
                            }, function(result) {
                                callback(null, result);
                            });
                        }, 5000);
                    });
                });
            };
        });

        async.series(tasks, function(err, results){
            console.log("Finished");
            ph.exit();
        });
    });
});

当然,您也可以将 phantom.create() 移到每个任务内,这将为每个请求创建一个单独的进程,但上面的代码会更快。

关于javascript - 如何使用 Phantomjs 设置页面抓取之间的时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32532279/

相关文章:

javascript - 什么是最可靠的CSS选择器来选择谷歌搜索结果?

javascript - 当我们为元素设置新的 html 时,如何保存事件处理程序?

javascript - 如何定位元素,使其不会流出可见屏幕

node.js - 关闭并重新打开终端后如何访问正在运行的nodemon脚本?

javascript - 为什么我的应用程序没有到达 console.log?

html - 使用xpath(python3)的href属性为空

go - 如何解析网页跨度中的值?

javascript - jQuery 多 ID 选择器通过变量连接

javascript - Flow 动态地将属性添加到 Error

node.js - typescript : "Cannot find module"具有有效的打字