本文為大家講解的是一個PhantomJS安裝方法,及快速入門教程,感興趣的同學參考下。
這裏有新鮮出爐的PhantomJS教程,程序狗速度看過來!
PhantomJS
PhantomJS 是一個基於WebKit的服務器端 JavaScript API。它全面支持web而不需瀏覽器支持,其快速,原生支持各種Web標準: DOM 處理, CSS 選擇器, JSON, Canvas, 和 SVG。PhantomJS可以用於頁面自動化,網絡監測,網頁截屏,以及無界面測試等。
本文為大家講解的是一個PhantomJS安裝方法,及快速入門教程,感興趣的同學參考下。
PhantomJS 是一個基於WebKit的服務器端 javascript API。它全面支持web而不需瀏覽器支持,其快速,原生支持各種Web標準: DOM 處理, CSS 選擇器, JSON, Canvas, 和 SVG。PhantomJS可以用於頁面自動化,網絡監測,網頁截屏,以及無界面測試等。
一、安裝
安裝包下載地址:http://phantomjs.org/download.html,包括Windows,Mac OS,linux版本,自行選擇對應 版本下載解壓即可(為方便使用,可自已為phantomjs設置環境變量),其中帶有一個example文件夾,裏面有很多已經寫好的代碼供使用。本文假設phantomjs已經安裝好並已設置了環境變量。
二、使用
Hello, World!
新建一個包含下面兩行腳本的文本文件:
console.log(
'Hello, world!'
);
phantom.exit();
將文件另存為hello.js
,然後執行它:
phantomjs hello.js
輸出結果為:Hello, world!
第一行將會在終端打印出字符串,第二行phantom.exit
將退出運行。
在該腳本中調用phantom.exit
是非常重要的,否則 PhantomJS 將根本不會停止。
腳本參數 – Script Arguments
Phantomjs如何傳遞參數呢?如下所示 :
phantomjs examples/arguments.js foo bar baz
其中的foo, bar, baz就是要傳遞的參數,如何獲取呢:
var
system = require(
'system'
);
if
(system.args.length === 1) {
console.log(
'Try to pass some args when invoking this script!'
);
}
else
{
system.args.forEach(
function
(arg, i) {
console.log(i +
': '
+ arg);
});
}
phantom.exit();
它將輸出 :
0: foo 1: bar 2: baz
頁面加載 – Page Loading
通過創建一個網頁對象,一個網頁可以被加載,分析和渲染。
下面的腳本將示例頁面對象最簡單的用法,它加載 example.com 並且將它保存為一張圖片,example.png
。
var
page = require(
'webpage'
).create();
page.open(
'http://example.com'
,
function
() {
page.render(
'example.png'
);
phantom.exit();
});
由於它的這個特性,PhantomJS 可以用來網頁截屏,截取一些內容的快照,比如將網頁、SVG存成圖片,PDF等,這個功能很牛X。
接下來的loadspeed.js
腳本加載一個特殊的URL (不要忘了http協議) 並且計量加載該頁面的時間。
var
page = require(
'webpage'
).create(),
system = require(
'system'
),
t, address;
if
(system.args.length === 1) {
console.log(
'Usage: loadspeed.js <some URL>'
);
phantom.exit();
}
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(
'<a title="Loading" href="http://www.domain.net/tag/loading">Loading</a> time '
+ t +
' msec'
);
}
phantom.exit();
});
在命令行運行該腳本:
phantomjs loadspeed.js http:
//www.Google.com
它輸出像下面的東西:
Loading http://www.google.com Loading time 719 msec
代碼運算 – Code Evaluation
要想在網頁的上下文中對JavaScript 或 CoffeeScript 進行運算,使用evaluate()
方法。代碼是在“沙箱”中運行的,它沒有辦法讀取在其所屬頁面上下文之外的任何JavaScript對象和變量。evaluate()
會返回一個對象,然而它僅限制於簡單的對象並且不能包含方法或閉包。
這有一個示例來顯示網頁標題:
var
page = require(
'webpage'
).create();
page.open(url,
function
(status) {
var
title = page.evaluate(
function
() {
return
document.title;
});
console.log(
'Page title is '
+ title);
});
任何來自於網頁並且包括來自evaluate()
內部代碼的控制臺信息,默認不會顯示的。要重寫這個行為,使用onConsolemessage
回調函數,前一個示例可以被改寫成:
var
page = require(
'webpage'
).create();
page.onConsoleMessage =
function
(msg) {
console.log(
'Page title is '
+ msg);
};
page.open(url,
function
(status) {
page.evaluate(
function
() {
console.log(document.title);
});
});
DOM操作 – DOM Manipulation
由於腳本好像是一個Web瀏覽器上運行的一樣,標準的DOM腳本和CSS選擇器可以很好的工作。這使得PhantomJS適合支持各種頁面自動化任務。
下面的 useragent.js
將讀取id 為myagent的元素的 textContent
屬性:
var
page = require(
'webpage'
).create();
console.log(
'The default user agent is '
+ page.settings.userAgent);
page.settings.userAgent =
'SpecialAgent'
;
page.open(
'http://www.httpuseragent.org'
,
function
(status) {
if
(status !==
'success'
) {
console.log(
'Unable to access Network'
);
}
else
{
var
ua = page.evaluate(
function
() {
return
document.getElementById(
'myagent'
).textContent;
});
console.log(ua);
}
phantom.exit();
});
上面示例同樣提供了一種自定義user agent
的方法。
使用JQuery及其他類庫:
var
page = require(
'webpage'
).create();
page.open(
'http://www.sample.com'
,
function
() {
page.includeJs(
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"
,
function
() {
page.evaluate(
function
() {
$(
"button"
).click();
});
phantom.exit()
});
});
網絡請求及響應 – Network Requests and Responses
將一個頁面從一臺遠程服務器請求一個資源的時候,請求和響應均可以通過onResourceRequested
和 onResourceReceived
回調方法追蹤到。示例 netlog.js:
var
page = require(
'webpage'
).create();
page.onResourceRequested =
function
(request) {
console.log(
'Request '
+ JSON.stringify(request, undefined, 4));
};
page.onResourceReceived =
function
(response) {
console.log(
'Receive '
+ JSON.stringify(response, undefined, 4));
};
page.open(url);
獲取如何把該特性用於HAR 輸出以及基於YSlow的性能分析的更多信息,請參閱網絡監控頁面。
三、參考
PhantomJs官網:http://phantomjs.org/
GitHub:https://github.com/ariya/phantomjs/wiki/Quick-Start
Tags: 下載地址 Windows 文本文件 瀏覽器 example
文章來源: