使用phantomJS刮取javascript网页

时间:2018-05-15 11:36:18

标签: javascript phantomjs

我正在尝试使用phantomJS抓取动态网页。下面是我试图抓取的代码和网址。该代码适用于其他网址,但这个代码总是作为空白的html文档返回。有谁知道如何解决这个问题?

我对javascript并不是很熟悉,所以这段代码是从其他地方复制的。我将超时时间从2.5秒增加到30秒,并没有什么区别。

var url ='https://www.amazon.com/gp/profile/amzn1.account.AFJ6MBZ5CSY4R6K4USNMQ7JWEQCA/';
var page = new WebPage()
var fs = require('fs');


page.open(url, function (status) {
        just_wait();
});

function just_wait() {
    setTimeout(function() {
            fs.write('page.html', page.content, 'w');
        phantom.exit();
    }, 30000);
}

1 个答案:

答案 0 :(得分:0)

这就是我解决这些问题的方法。

<强> app.js

list(a)

<强> settings.js

var url ='https://www.amazon.com/gp/profile/amzn1.account.AFJ6MBZ5CSY4R6K4USNMQ7JWEQCA/';
var steps=[];
var testindex = 0;
var loadInProgress = false;

//This is set to true when a page is still loading
/*********SETTINGS*********************/
var settings   = require('./settings');
var webPage = require('webpage');
var page = webPage.create();
var fs = require('fs');
page.settings.userAgent = settings.userAgents.desktop;
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;
//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;

page.viewportSize = {
  width: settings.viewport.desktop.width,
  height: settings.viewport.desktop.height
};

/*********SETTINGS END*****************/
console.log('All settings Loaded, Start With Execution');
/**********DEFINE STEPS THAT PHANTOM SHOULD DO***********************/
steps = [
    function(){
      console.log("Step 1 - Load Page => "+url);
      page.open(url, function(status){
        if(status === 'success'){
          console.log('Loaded');
        }else{
          console.log('Error Loading Page. Try Logging In Again');
          phantom.exit(0);
        }
      });
    },
    function(){
      page.render('./test.png');
    },
];

/**********END STEPS THAT PHANTOM SHOULD DO***********************/
interval = setInterval(executeRequestsStepByStep, 3000);
function executeRequestsStepByStep(){
  if(loadInProgress == false && typeof steps[testindex] == "function") {
    steps[testindex]();
    testindex++;
    return;
  }

  if(typeof steps[testindex] != "function") {
    console.log("Quiting");
    fs.write('page.html', page.content, 'w');
    phantom.exit(0);
  }
}
/*
 * These listeners are very important in order to phantom work properly.
 * Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
 * Without this, we will get content of the page, even a page is not fully loaded.
 */

page.onLoadStarted = function() {
    loadInProgress = true;
};
page.onLoadFinished = function() {
    loadInProgress = false;
};
page.onConsoleMessage = function(msg) {
    // console.log(msg);
};

phantom.onError = function(msg, trace) {
  var msgStack = ['PHANTOM ERROR: ' + msg];
  if (trace && trace.length) {
    msgStack.push('TRACE:');
    trace.forEach(function(t) {
      msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
    });
  }
  console.error(msgStack.join('\n'));
  phantom.exit(1);
};

我已经测试了这个,这很好。