我正在使用以下基于loadspeed.js示例的代码打开一个https://站点,该站点也需要http服务器身份验证.
var page = require('webpage').create(), system = require('system'), t, address;
page.settings.userName = 'myusername';
page.settings.password = 'mypassword';
if (system.args.length === 1) {
console.log('Usage: scrape.js <some URL>');
phantom.exit();
} else {
t = Date.now();
address = system.args[1];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Page title is ' + page.evaluate(function () {
return document.title;
}));
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
} …Run Code Online (Sandbox Code Playgroud) 打开网页并截取屏幕截图.
只使用phantomjs:(这是一个简单的脚本,实际上它是在他们的文档中使用的示例脚本.http://phantomjs.org/screen-capture.html
var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)
问题是,对于一些网站(如github),足够有趣的是以某种方式检测并且不提供phantomjs并且没有任何东西被渲染.结果是github.png一个空白的白色png文件.
用说:"google.com"替换github,你会得到一个很好的(正确的)截图.
起初我认为这是一个Phantomjs问题所以我尝试通过Casperjs运行它:
casper.start('http://www.github.com/', function() {
this.captureSelector('github.png', 'body');
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
但我得到与Phantomjs相同的行为.
所以我认为这很可能是用户代理问题.如:Github嗅出Phantomjs并决定不显示页面.所以我设置用户代理如下,但仍然无法正常工作.
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)
所以我试图解析页面,显然有些网站(再次像github)似乎没有发送任何网络.
使用casperjs我试图打印标题.对于google.com我回来了Google但是对于github.com我回来了bupkis.示例代码:
var casper = require('casper').create();
casper.start('http://github.com/', function() {
this.echo(this.getTitle());
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
与上述相同也在纯粹的幻影中产生相同的结果.
这可能是一个时间问题吗?github只是超级慢吗?我怀疑它,但无论如何都要测试..
var page = require('webpage').create();
page.open('http://github.com', function (status) {
/* irrelevant */ …Run Code Online (Sandbox Code Playgroud) 我的https页面有问题.页面完全正常,它存在,但phantomjs告诉我别的东西:'加载资源失败,状态失败'.我读了一段时间,现在我知道它是phantomjs的bug,这个问题的解决方案是:
--ignore-ssl-errors=true
Run Code Online (Sandbox Code Playgroud)
所以我知道解决方案,但不知道如何使用它.我如何将这个传递给casper的phantomjs?我该怎么办?
编辑:
整个代码:
var casper = require('casper').create({
verbose: true,
logLevel: 'warning',
pageSettings: { javascriptEnabled: true },
viewportSize: {width: 1024, height: 768}
});
var url = 'http://us3.php.net/manual/en/function.explode.php',
xp = require('casper').selectXPath;
// ### AKCJE PODSTAWOWE ###
casper.start(url);
casper.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0');
casper.then(function(){this.captureSelector('logi/img1.png', 'body');});
casper.then(function(){ this.sendKeys(xp('/html/body/nav/div/div/div/form/span/input[2]'),'test18');});
casper.then(function(){this.captureSelector('logi/img2.png', 'body');})
casper.thenClick(xp('/html/body/div[3]/div/section/div/div[2]/a[1]'));
casper.wait(2000);
casper.then(function(){this.captureSelector('logi/img3.png', 'body');})
casper.run(function(){ this.exit(); });
Run Code Online (Sandbox Code Playgroud) 我有一个casperjs脚本,当我在linux服务器上运行时会给出所需的结果,但是当我从笔记本电脑运行时,它不起作用.
我该如何调试?工作日志:
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://caspertest.grsrv.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://caspertest.grsrv.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/"
[debug] [phantom] Navigation requested: url=https://caspertest.grsrv.com/my_app/#/auth, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://caspertest.grsrv.com/my_app/#/auth"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://caspertest.grsrv.com/my_app/#/auth (HTTP 200)
[info] [remote] attempting …Run Code Online (Sandbox Code Playgroud) 我通过npm在我的windows machina上安装了casperjs和phantomjs.但是我得到了这个问题.
C:\>casperjs sample.js
C:\>Unable to open file: sample.js
Unsafe JavaScript attempt to access frame with URL about:blank from frame
with URL file:///C:/Users/vini/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap.js.
Domains, protocols and ports must match.
Run Code Online (Sandbox Code Playgroud)