PhantomJS - open()

open 方法用于打开网页。open 方法接受页面 URL 并具有回调函数,该函数在页面加载时调用。回调函数是可选的,可以在需要时使用。回调函数包含状态,该状态定义页面的成功或失败。

语法

其语法如下 −

var wpage = require('webpage').create();
wpage.open(url, function(status) {
    //status 是成功或失败
}); 

使用 GET 方法 open()

var wpage = require('webpage').create();  
wpage.open('http://www.google.com/', function(status) { 
   console.log(status); 
   phantom.exit(); 
}); 

上述程序生成以下输出

Success

使用 POST 方法 open()

var wpage = require('webpage').create(); 
var postdata = "username = roy"; 
wpage.open('http://localhost/tasks/a.php', 'POST',postdata, function(status) { 
   console.log(status); 
   console.log(wpage.content); 
   phantom.exit(); 
});

a.php

<?php 
   print_r($_POST); 
?> 

上述程序生成以下输出

success 
<html><head></head><body>Array 
( 
   [username] => roy 
) 
</body></html>

phantomjs_webpage_module_methods.html