PhantomJS - open()



open 方法用於開啟網頁。open 方法採用頁面 URL,並有一個回撥函式,在頁面載入時呼叫該函式。回撥函式是可選的,可根據需要使用。回撥函式包含的狀態用於定義頁面的成功或失敗。

語法

其語法如下所示 −

var wpage = require('webpage').create(); 
wpage.open(url, function(status) { 
   //status is success or failure 
}); 

使用 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('https:///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.htm
廣告
© . All rights reserved.