我正在尝试使用像这样的phantomjs获取网站名称build.com的页面:
var page = require('webpage').create();
var fs = require('fs');
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36';
page.onInitialized = function ()
{
page.customHeaders = {
"Accept-Language": "en-US,en;q=0.8",
"Upgrade-Insecure-Requests": "1",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
};
page.viewportSize = {
width: 1280,
height: 1024
};
};
page.evaluate(function () {});
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
page.open('https://www.build.com/millennium-lighting-rwhc17-pendant-light/s875082', function (status)
{
if (status == 'success')
{}
page.render('export.png');
fs.write('1.html', page.content, 'w');
phantom.exit();
});
但我得到一个空白页面。为什么会这样?我在这里做错了吗?请帮忙。我是phantomjs世界的新手:)
尝试使用不同版本的phantomJS,但仍有相同的行为:(
从this链接
下载了phantomJS当击中几次(最多4-5次)时,我会得到正确的截图和页面,但不止于此,我会得到空白页。
控制台输出包含:
Unable to load resource (#6 URL:https://www.build.com/blddstlfjwbtysftuzqdbvevbwdr.js?PID=BD9E1747-99DF-3DE1-AF48-65E96064EC7E)
Error code: 5. Description: Operation canceled
此外,状态出现了"失败"与成功"相比4次。