phantomjs 中文文檔 轉載
入門教程:轉載 http://www.cnblogs.com/front-Thinking/p/4321720.html
1.介紹
簡介
PhantomJS是一個基於webkit的JavaScript API。它使用QtWebKit作為它核心瀏覽器的功能,使用webkit來編譯解釋執行JavaScript代碼。任何你可以在基於webkit瀏覽器做的事情,它都能做到。它不僅是個隱形的瀏覽器,提供了諸如CSS選擇器、支持Web標准、DOM操作、JSON、HTML5、Canvas、SVG等,同時也提供了處理文件I/O的操作,從而使你可以向操作系統讀寫文件等。PhantomJS的用處可謂非常廣泛,諸如網絡監測、網頁截屏、無需瀏覽器的 Web 測試、頁面訪問自動化等。
地址
PhantomJS官方地址:http://phantomjs.org/。
PhantomJS官方API:http://phantomjs.org/api/。
PhantomJS官方示例:http://phantomjs.org/examples/。
PhantomJS GitHub:https://github.com/ariya/phantomjs/。
2.模塊目錄
Command Line Interface
phantom Object
Web Page Module
Child Process Module
File System Module
System Module
Web Server Module
3.api目錄
執行腳本
phantomjs [options] somescript.js [arg1 [arg2 […]]]
如果后面沒有任何參數, phantomjs將會進入交互模式
Command-line Options 命令行參數
- –help or -h
lists all possible command-line options. Halts immediately, will not run a script passed as argument.
列出所有的命令行選項.不執行之后的腳本文件.
- –version or -v
prints out the version of PhantomJS. Halts immediately, will not run a script passed as argument.
打印出其版本.不執行之后的腳本文件.
- –cookies-file=/path/to/cookies.txt
specifies the file name to store the persistent Cookies.
指定持久儲存cookies的文件.
- –disk-cache=[true|false]
enables disk cache (at desktop services cache storage location, default is false). Also accepted: [yes|no].
啟用磁盤高速緩存(在桌面服務緩存的存儲位置,默認為false)也可接收[yes][no]
- –ignore-ssl-errors=[true|false]
ignores SSL errors, such as expired or self-signed certificate errors (default is false). Also accepted: [yes|no].
忽略SSL錯誤,比如過期或自簽名的證書錯誤。(默認為false)也可接收[yes][no]
- –load-images=[true|false]
load all inlined images (default is true). Also accepted: [yes|no].
加載所有圖片。(默認為true)也可接收[yes][no]
- –local-storage-path=/some/path
path to save LocalStorage content and WebSQL content.
儲存LocalStorage和WebSQL內容的文件路徑。
- –local-storage-quota=number
maximum size to allow for data.
允許數據的最大長度。
- –local-to-remote-url-access=[true|false]
allows local content to access remote URL (default is false). Also accepted: [yes|no].
允許本地內容訪問遠程URL,默認為false。也可接收[yes][no]。
- –max-disk-cache-size=size
limits the size of disk cache (in KB).
限制磁盤緩存大小(單位為kb)。
- –output-encoding=encoding
sets the encoding used for terminal output (default is utf8).
設置終端輸出編碼格式(默認為utf-8)
- –remote-debugger-port
starts the script in a debug harness and listens on the specified port
調試腳本端口
- –remote-debugger-autorun
runs the script in the debugger immediately: ‘yes’ or ‘no’ (default)
在debugger時候立刻運行腳本
- –proxy=address:port
specifies the proxy server to use (e.g. –proxy=192.168.1.42:8080).
指定要使用的代理服務器。
- –proxy-type=[http|socks5|none]
specifies the type of the proxy server (default is http).
指定代理服務器類型(默認http)
- –proxy-auth
specifies the authentication information for the proxy, e.g. –proxy-auth=username:password).
指定代理的認證信息
- –script-encoding=encoding
sets the encoding used for the starting script (default is utf8).
設置腳本的啟動編碼(默認utf-8)
- –ssl-protocol=[sslv3|sslv2|tlsv1|any’]
sets the SSL protocol for secure connections (default is SSLv3).
為安全連接建立SSL協議(默認SSLv3)
- –ssl-certificates-path=
Sets the location for custom CA certificates (if none set, uses system default).
設置自定義CA證書的位置(設置為空則使用系統默認)
- –web-security=[true|false]
enables web security and forbids cross-domain XHR (default is true). Also accepted: [yes|no].
開啟網絡安全,並禁止跨域XHR(默認開啟)也可接收[yes][no]。
- –webdriver(暫未翻譯)
starts in ‘Remote WebDriver mode’ (embedded GhostDriver): ‘[[:]]’ (default ‘127.0.0.1:8910’)
- –webdriver-selenium-grid-hub (暫未翻譯)
URL to the Selenium Grid HUB: ‘URLTOHUB’ (default ‘none’) (NOTE: works only together with ‘–webdriver’)
自從1.3版本,可以新建一個json文件,將以上配置寫入其中,並以這個配置文件啟動:
- –config=/path/to/config.json
請注意:這個json文件中上述選項寫法略有所不同:去掉所有的虛線,並且按照駝峰命名法來書寫。並且部分選項名稱有所變化。見下例:
{
/* Same as: –ignore-ssl-errors=true */
"ignoreSslErrors": true,
/* Same as: –max-disk-cache-size=1000 */
"maxDiskCacheSize": 1000,
/* Same as: –output-encoding=utf8 */
"outputEncoding": “utf8”
/* etc. */
}
未直接翻譯的選項名稱:
-
–disk-cache => diskCacheEnabled
-
–load-images => autoLoadImages
-
–local-storage-path => offlineStoragePath
-
–local-storage-quota => offlineStorageDefaultQuota
-
–local-to-remote-url-access => localToRemoteUrlAccessEnabled
-
–web-security => webSecurityEnabled
phantom Object phantom 對象
- phantom.cookies {Object[]}
Get or set Cookies for any domain (though, for setting, use of phantom.addCookie is preferred). These Cookies are stored in the CookieJar and will be supplied when opening pertinent WebPages.
設置或獲取cookies。(用於設置時,其值會被phantom.addCookie設置的值覆蓋)。Cookies儲存在CookieJar中,在打開相關頁面時被使用。
This array will be pre-populated by any existing Cookie data stored in the cookie file specified in the PhantomJS startup config/command-line options, if any.
這個數列將被在config文件或是命令行中設定的cookie文件數據預填充。
- phantom.cookiesEnabled {Boolean}
Controls whether the CookieJar is enabled or not. Defaults to true.
控制CookieJar是否開啟。默認true。
- phantom.libraryPath {String}
This property stores the path which is used by injectJs function to resolve the script name. Initially it is set to the location of the script invoked by PhantomJS.
儲存injectJs函數注入腳本時的位置。最初BALABALA……(沒用了)。
- phantom.version {Object}
Read-only. The version of the executing PhantomJS instance. Example value: { ‘major’: 1, ‘minor’: 7, ‘patch’: 0 }.
只讀。執行phantomJs實例的版本。
- addCookie(Object) {Boolean}
Add a Cookie to the CookieJar. Returns true if successfully added, otherwise false.
在CookJar中加入一個Cookie。成功返回true,否則返回false。
phantom.addCookie({
'name': 'Added-Cookie-Name',
'value': 'Added-Cookie-Value',
'domain': '.google.com'
});
- clearCookies() {void}
Delete all Cookies in the CookieJar.
刪除CookieJar中的Cookies。
- deleteCookie(cookieName) {Boolean}
Delete any Cookies in the CookieJar with a ‘name’ property matching cookieName. Returns true if successfully deleted, otherwise false.
刪除與名稱相配的Cookies。成功返回true,否則返回false。
- phantom.exit(returnValue) {void}
Exits the program with the specified return value. If no return value is specified, it is set to 0.
帶有指定返回值的結束程序。默認為0.
if (somethingIsWrong) {
phantom.exit(1);
} else {
phantom.exit(0);
}
- phantom.injectJs(filename) {boolean}
Injects external script code from the specified file into the Phantom outer space. If the file cannot be found in the current directory, libraryPath is used for additional look up. This function returns true if injection is successful, otherwise it returns false.
從Phantom外部空間注入指定文件的腳本代碼。如果當前目錄下未發現,將從libraryPath指定的位置查找。注入成功返回true,失敗返回false。
var wasSuccessful = phantom.injectJs(‘lib/utils.js’);
- page.onError
This callback is invoked when there is a JavaScript execution error not caught by a page.onError handler. This is the closest it gets to having a global error handler in PhantomJS, and so it is a best practice to set this onError handler up in order to catch any unexpected problems. The arguments passed to the callback are the error message and the stack trace [as an Array].
當頁面有未能處理的腳本錯誤時調用這個回調函數。這個函數最先得到phantomJs中處理的全局錯誤,所以最好的方法是設置出錯處理程序以捕捉任何意想不到的問題。傳遞的參數是錯誤消息和堆棧追蹤(其為一個數組)。
phantom.onError = function (msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function (t) {
msgStack.push(' -> ' +
(t.file || t.sourceURL) + ': ' + t.line +
(t.function ? ' (in function ' + t.function + ')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};
Web Page Module Web頁面模塊
To start using, you must require a reference to the webpage module then use it to create an instance:
開始使用之前,你必須請求一個webpage模塊的引用,並創造一個實例:
var webPage = require('webpage');
var page = webPage.create();
canGoBack
canGoForward
- clipRect {object}
This property defines the rectangular area of the web page to be rasterized when page.render is invoked. If no clipping rectangle is set, page.render will process the entire web page.
這個屬性定義了一個網頁的矩形裁剪區域。供page.render函數調用。如果沒有設置裁剪區域,page.render將處理整個網頁。
var webPage = require('webpage');
var page = webPage.create();
page.clipRect = {
top: 14,
left: 3,
width: 400,
height: 300
};
- content {string}
This property stores the content of the web page (main frame), enclosed in an HTML/XML element. Setting the property will effectively reload the web page with the new content.
See also page.plainText to get the content without any HTML tags.
此屬性存儲網頁的內容(主要frame),包裹在一個HTML/XML元素中。設置這個屬性將有效地以新內容重新加載網頁。
Page.plainText將獲得沒有HTML標簽的內容。
var webPage = require('webpage');
var page = webPage.create();
page.open('http://phantomjs.org', function (status) {
var content = page.content;
console.log('Content: ' + content);
phantom.exit();
});
- Cookies
Get or set Cookies visible to the current URL (though, for setting, use of page.addCookie is preferred). This array will be pre-populated by any existing Cookie data visible to this URL that is stored in the CookieJar, if any.
對當前URL設置Cookie可見或獲取之,(然而就設置而言更推薦使用page.addcookie)。如果cookieJar中有cookie,並且對此URL可見,那么這些cookie數據將會預加載。
Cookies is an array of objects: javascript { domain: ‘example.com’, expires: ‘Sat Oct 11 2014 21:44:33 GMT+0200 (CEST)’, expiry: 1476128618, httponly: false, name: ‘cookieName’, path: ‘/’, secure: false, value: cookieValue }
- customHeaders {object}
This property specifies additional HTTP request headers that will be sent to the server for every request issued (for pages and resources). The default value is an empty object {}. Headers names and values get encoded in US-ASCII before being sent. Please note that the ‘User-Agent’ should be set using the page.settings, setting the ‘User-Agent’ property in this property will overwrite the value set via page.settings.
這個屬性為每一個發出的請求指定額外的發送到服務器HTTP請求頭(頁面和資源)。默認值是一個空對象{}。Headers發送之前會用US_ASCII碼編碼。請注意:’User-Agent’應該使用page.settings來設置;如果設置’User-Agent’屬性,則通過page.settings設置的屬性將被重寫。
var webPage = require('webpage');
var page = webPage.create();
page.customHeaders = {
"X-Test": "foo",
"DNT": "1"
};
Do you only want these customHeaders passed to the initial page.open request?
你只想讓這些headers只傳遞到初始page.open請求嗎?
var webPage = require('webpage');
var page = webPage.create();
page.customHeaders = {
"X-Test": "foo",
"DNT": "1"
};
page.onInitialized = function() {
page.customHeaders = {};
};
- Event
focusedFrameName - frameContent {string}
This property stores the content of the web page’s currently active frame (which may or may not be the main frame), enclosed in an HTML/XML element.
Setting the property will effectively reload the web page with the new content.
此屬性存儲頁面當前活動的frame內容(這可能不是主要frame),包裹在一個html/xml元素中。
設置這個屬性將會用新的內容有效地重新加載網頁。
- frameName
framePlainText {string}
Read-only. This property stores the content of the web page’s currently active frame (which may or may not be the main frame) as plain text — no element tags!
只讀。這個屬性存儲頁面當前活動的frame內容(這可能不是主要框架)——純文本,沒有元素標簽。
frameTitle
- frameUrl {string}
Read-only. This property gets the current URL of the web page’s currently active frame (which may or may not be the main frame).
只讀。此屬性獲取當前Web頁面活動frame的URL,(這個frame可能不是主要的frame)
- framesCount
framesName
Returns array with frames names.
返回frame名稱組成的數組。
- libraryPath {string}
This property stores the path which is used by page.injectJs function to resolve the script name.
Initially it is set to the location of the script invoked by PhantomJS.
此屬性存儲由page.injectjs函數調用腳本的路徑名。最初balabala……
- navigationLocked {boolean}
This property defines whether navigation away from the page is permitted or not. If it is set to true, then the page is locked to the current URL. Defaults to false.
這個屬性定義是否允許離開頁面。如果設置為true,那么頁面將被鎖定到當前URL。默認值為false。
- offlineStoragePath
- offlineStorageQuota
- ownsPages
- pages
- pagesWindowName
- paperSize {object}
This property defines the size of the web page when rendered as a PDF.
這個屬性定義網頁呈現為一個PDF時的尺寸。
- plainText {string}
Read-only. This property stores the content of the web page (main frame) as plain text — no element tags!
See also: page.content which returns the content with element tags.
只讀。此屬性存儲網頁(主frame)內容為純文本——沒有元素標簽。
參見:page.content返回帶元素標簽的內容。
- scrollPosition {object}
This property defines the scroll position of the web page.
這個屬性定義網頁的滾動位置。
var webPage = require('webpage');
var page = webPage.create();
page.scrollPosition = {
top: 100,
left: 0
};
- settings {object}
This property stores various settings of the web page:
此屬性存儲網頁的各種設置:
javascriptEnabled defines whether to execute the script in the page or not (defaults to true).
確定是否在頁面執行腳本(默認為true)
loadImages defines whether to load the inlined images or not (defaults to true).
確定是否加載圖片(默認為true)
localToRemoteUrlAccessEnabled defines whether local resource (e.g. from file) can access remote URLs or not (defaults to false).
確定本地資源(如文件資源)可以訪問遠程URL(默認為false)
userAgent defines the user agent sent to server when the web page requests resources.
定義當page請求資源時發送給服務器的user-agent。
userName sets the user name used for HTTP authentication.
設置用於http授權的用戶姓名。
password sets the password used for HTTP authentication.
設置用於http授權的用戶密碼。
XSSAuditingEnabled defines whether load requests should be monitored for cross-site scripting attempts (defaults to false).
定義是否監控加載的請求以防其為跨站腳本攻擊。(默認為false)
webSecurityEnabled defines whether web security should be enabled or not (defaults to true).
定義是否開啟網頁安全(默認為true)
resourceTimeout (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout.
超時時間(毫秒)任何資源請求在超時以后停止,並繼續進行其余頁面的部分。onResourceTimeout回調函數將會在超時以后被調用。
Note: The settings apply only during the initial call to the page.open function. Subsequent modification of the settings object will not have any impact.
注意:該設置僅適用於page.open函數初始化期間。后續修改不會受到此影響。
var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36’;
- title
page.open('http://phantomjs.org', function (status) {
console.log(page.title); // get page Title
phantom.exit();
});
- url {string}
Read-only. This property gets the current URL of the web page (main frame).
var webPage = require('webpage');
var page = webPage.create();
page.open('http://phantomjs.org', function (status) {
var url = page.url;
console.log('URL: ' + url);
phantom.exit();
});
- viewportSize {object}
This property sets the size of the viewport for the layout process. It is useful to set the preferred initial size before loading the page, e.g. to choose between ‘landscape’ vs ‘portrait’.
這個屬性設置布局過程中的視窗大小。這被用於在加載頁面之前首選的初始值大小。例如從landsscape和portrait之前選擇。
Because PhantomJS is headless (nothing is shown), viewportSize effectively simulates the size of the window like in a traditional browser.
因為Phantomjs是個無界面瀏覽器(不顯示任何東西),
- windowName
zoomFactor {number}
This property specifies the scaling factor for the page.render and page.renderBase64 functions.
The default is 1, i.e. 100% zoom.
這個縮放因子指定page.render和page.renderBase64函數的縮放程度。默認為1,即百分之百縮放。
var webPage = require('webpage');
var page = webPage.create();
page.zoomFactor = 0.25;
page.render('capture.png');
- addCookie(Cookie) {boolean}
Add a Cookie to the page. If the domain does not match the current page, the Cookie will be ignored/rejected. Returns true if successfully added, otherwise false.
給頁面添加一個cookie。如果域名不匹配當前頁,cookie將被忽略或拒絕。如果成功返回true。否則false。
var webPage = require('webpage');
var page = webPage.create();
phantom.addCookie({
'name' : 'Valid-Cookie-Name', /* required property */
'value' : 'Valid-Cookie-Value', /* required property */
'domain' : 'localhost',
'path' : '/foo', /* required property */
'httponly' : true,
'secure' : false,
'expires' : (new Date()).getTime() + (1000 * 60 * 60) /* <– expires in 1 hour */
});
- clearCookies() {void}
Delete all Cookies visible to the current URL.
刪除對當前URL可見的cookie。
- close() {void}
Close the page and releases the memory heap associated with it. Do not use the page instance after calling this.
關閉page,釋放相關的內存。調用這個方法以后不再使用這個頁面實例。
Due to some technical limitations, the web page object might not be completely garbage collected. This is often encountered when the same object is used over and over again. Calling this function may stop the increasing heap allocation.
由於一些技術上的限制,網頁對象的垃圾收集可能不完全,導致經常出現同一對象的反復調用。調用此函數可以阻止越來越多的內存分配。
- deleteCookie(cookieName) {boolean}
Delete any Cookies visible to the current URL with a ‘name’ property matching cookieName. Returns true if successfully deleted, otherwise false.
刪除一個對當前URL可見的,name屬性為cookieName的cookie。成功返回true,否則false。
var webPage = require('webpage');
var page = webPage.create();
page.deleteCookie('Added-Cookie-Name');
evaluate(function, arg1, arg2, …) {object}
Evaluates the given function in the context of the web page. The execution is sandboxed, the web page has no access to the phantom object and it can’t probe its own setting.
在當前Web頁面上下文中執行給予的函數。這個函數執行在沙箱中,web頁面無法訪問phantom的上下文和設置。
Get the page title from Bing.com (1)
var webPage = require('webpage');
var page = webPage.create();
page.open('http://m.bing.com', function(status) {
var title = page.evaluate(function() {
return document.title;
});
console.log(title);
phantom.exit();
});
Get the page title from Bing.com (2)
As of PhantomJS 1.6, JSON-serializable arguments can be passed to the function. In the following example, the text value of a DOM element is extracted.
(自從phantom1.6)Json字符串可以傳遞給函數。在下面的例子中 ,一個dom元素的text value被提出了出來。
The following example achieves the same end goal as the previous example but the element is chosen based on a selector which is passed to the evaluate call:
下面的例子實現和之前的例子相同的目標,然而這個元素是基於一個傳遞到evaluate中的選擇器選擇的。
page.open('http://m.bing.com', function(status) {
var title = page.evaluate(function(s) {
return document.querySelector(s).innerText;
}, 'title');
console.log(title);
phantom.exit();
});
Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.
Note:傳給evaluate的參數和返回值必須是一個簡單的原始對象。經驗之談:如果它可以序列化成json,那么它就是合適的。
Closures, functions, DOM nodes, etc. will not work!
閉包,函數,DOM節點等等都是無法工作的!
- evaluateAsync(function) {void}
Evaluates the given function in the context of the web page without blocking the current execution. The function returns immediately and there is no return value. This is useful to run some script asynchronously.
在web頁面上下文無阻塞立刻執行給定的函數。該函數立刻返回,沒有返回值。在異步執行一些腳本時有用。
- getPage(windowName)
- go(index)
- goBack()
- goForward()
- includeJs(url, callback) {void}
Includes external script from the specified url (usually a remote location) on the page and executes the callback upon completion.
將指定URL(通常是一個遠程地址)上的腳本包括進這個page,完成后回調。
var webPage = require('webpage');
var page = webPage.create();
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', function() {
// jQuery is loaded, now manipulate the DOM
var $loginForm = $('form#login');
$loginForm.find('input[name="username"]').value('phantomjs');
$loginForm.find('input[name="password"]').value('c45p3r');
});
- injectJs(filename) {boolean}
Injects external script code from the specified file into the page (like page.includeJs, except that the file does not need to be accessible from the hosted page).
If the file cannot be found in the current directory, libraryPath is used for additional look up.
This function returns true if injection is successful, otherwise it returns false.
以特殊文件向頁面注入腳本代碼(類似page.indudejs,除了這個文件不需要從hosted page獲取。)
如果不能在當前文件夾中找到文件,程序將會去libraryPath路徑下尋找。
如果注入成功,返回true;否則返回false。
var webPage = require('webpage');
var page = webPage.create();
page.open('http://www.phantomjs.org', function(status) {
if (status === "success") {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
if (page.injectJs('do.js')) {
var title = page.evaluate(function() {
// returnTitle is a function loaded from our do.js file – see below
return returnTitle();
});
console.log(title);
phantom.exit();
}
});
}
});
- open(url, callback) {void} open(url, method, callback) {void} * open(url, method, data, callback) {void} open(url, settings, callback) {void}
Opens the url and loads it to the page. Once the page is loaded, the optional callback is called using page.onLoadFinished, and also provides the page status to the function (‘success’ or ‘fail’).
打開並加載URL到頁面。一旦加載完頁面,使用page.onloadFinished調用可能會有的回調。
並且提供給該函數success或者fail的狀態。
GET google.com and report “success” or “fail”:
var webPage = require('webpage');
var page = webPage.create();
page.open('http://www.google.com/', function(status) {
console.log('Status: ' + status);
// Do other things here…
});
POST data to google.com and report “success” or “fail”:
As of PhantomJS 1.2, the open function can be used to request a URL with methods other than GET. This syntax also includes the ability to specify data to be sent with the request. In the following example, we make a request using the POST method, and include some basic data.
自從1.2版本開始,open函數還可用於不止GET方式請求URL。這個語法還包括傳輸數據給葯發送的請求的能力。下面是一個使用POST方法的例子,包括一些基本的數據。
var webPage = require('webpage');
var page = webPage.create();
var postBody = 'user=username&password=password';
page.open('http://www.google.com/', 'POST', postBody, function(status) {
console.log('Status: ' + status);
// Do other things here…
});
POST json data to your.custom.api in utf-8 encoding:
As of PhantomJS 1.9, the open function can get an object of settings. and with a use of “encoding” key, you can set the custom encoding to your app. In this example, we’ve set the encoding to UTF8, and set the Content-Type header to application/json for making our server know the request has information in json format and not in urlencoded format.
從1.9版本開始,open函數可以得到一個settings對象。使用“encoding”鍵,你可以設置應用的自定義編碼。在這個例子中,我們設置編碼為“utf-8”並且為application/json設置Conten-Type header,以便於讓服務端明白這個請求Json格式,不出現亂碼。
var webPage = require('webpage');
var page = webPage.create();
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({
some: "data",
another: ["custom", "data"]
})
};
page.open('http://your.custom.api', settings, function(status) {
console.log('Status:' + status);
// Do other things here…
});
- openUrl(url, httpConf, settings)
reload
render(filename [, {format, quality}]) {void}
Renders the web page to an image buffer and saves it as the specified filename.
Currently, the output format is automatically set based on the file extension.
將網頁保存為指定文件名的突變,目前輸出格式自動設置為文件擴展名。
Supported formats
PNG GIF JPEG PDF
Quality
An integer between 0 and 100.
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1920, height: 1080 };
page.open("http://www.google.com", function start(status) {
page.render('google_home.jpeg', {format: 'jpeg', quality: '100'});
phantom.exit();
});
- renderBase64(format)
Renders the web page to an image buffer and returns the result as a Base64-encoded string representation of that image.
將網頁保存成圖片buffer,並以base64編碼字符串返回該圖片。
Supported formats
PNG GIF JPEG
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = {
width: 1920,
height: 1080
};
page.open('http://phantomjs.org', function (status) {
var base64 = page.renderBase64('PNG');
console.log(base64);
phantom.exit();
});
- sendEvent
Sends an event to the web page.
給頁面發送個事件。
The events are not synthetic DOM events, each event is sent to the web page as if it comes as part of user interaction.
這個事件並不是 虛擬DOM事件,每一個事件被當作用戶的交互發送到web頁。
- Mouse events
sendEvent(mouseEventType[, mouseX, mouseY, button=’left’])
The first argument is the event type. Supported types are ‘mouseup’, ‘mousedown’, ‘mousemove’, ‘doubleclick’ and ‘click’. The next two arguments are optional but represent the mouse position for the event.
The button parameter (defaults to left) specifies the button to push.
For ‘mousemove’, however, there is no button pressed (i.e. it is not dragging).
第一個參數是事件類型。支持的類型有“mouseup”,“mousedown”,“mousemove”,“doubleclick”和“click”。接下來的兩個可選參數代表了這個事件的鼠標位置。
Button參數(默認為左)規定了哪個按鈕被按下。
然而對於“mousemove”事件是沒有按鈕被按下的。(不是拖拽。)
- Keyboard events
sendEvent(keyboardEventType, keyOrKeys, [null, null, modifier])
The first argument is the event type. The supported types are: keyup, keypress and keydown. The second parameter is a key (from page.event.key), or a string.
第一個參數是事件類型。支持的類型有:“keyup”“keypress”和“keydown”。第二個參數是個按鍵(通過page.event.key),或者一個string.
You can also indicate a fifth argument, which is an integer indicating the modifier key.
你也可以加第五個參數,它是一個用來修飾key的整數。
0: No modifier key is pressed
0x02000000: A Shift key on the keyboard is pressed
0x04000000: A Ctrl key on the keyboard is pressed
0x08000000: An Alt key on the keyboard is pressed
0x10000000: A Meta key on the keyboard is pressed
0x20000000: A keypad button is pressed
Third and fourth argument are not taken account for keyboard events. Just give null for them.
第三個和第四個參數不對鍵盤事件起作用,給他們設置為null好了。
var webPage = require('webpage');
var page = webPage.create();
page.sendEvent('keypress', page.event.key.A, null, null, 0x02000000 | 0x08000000);
- setContent
Allows to set both page.content and page.url properties.
The webpage will be reloaded with the new content and the current location set as the given url, without any actual http request being made.
允許同時設置page.Content和page.url的屬性。網頁會重新加載一個新的內容,並且當前url被設置為被給予的URL。
- Stop
switchToFocusedFrame
switchToFrame(frameName) or switchToFrame(framePosition)
switchToMainFrame
switchToParentFrame
uploadFile(selector, filename)
Uploads the specified file (filename) to the form element associated with the selector.
將指定的文件(文件名)上傳給選擇器選擇的form元素。
This function is used to automate the upload of a file, which is usually handled with a file dialog in a traditional browser. Since there is no dialog in this headless mode, such an upload mechanism is handled via this special function instead.
這個函數用來執行一個文件上傳。這通常通過傳統瀏覽器里的一個文件對話框處理。既然這個瀏覽器沒有界面,這樣一個上傳機制就由一個函數所代替。
var webPage = require('webpage');
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');
- Callback Triggers
These functions call callbacks, used for tests…
closing(page)
initialized()
javaScriptAlertSent(message)
javaScriptConsoleMessageSent(message)
loadFinished(status)
loadStarted()
navigationRequested(url, navigationType, navigationLocked, isMainFrame)
rawPageCreated(page)
resourceError(resource)
resourceReceived(request)
resourceRequested(resource)
urlChanged(url)
- onAlert
This callback is invoked when there is a JavaScript alert on the web page. The only argument passed to the callback is the string for the message. There is no return value expected from the callback handler.
當web page有javascript 的alert()出現時調用此回調。唯一的傳遞給回調的參數是消息的字符串。這個回調函數沒有任何返回。
var webPage = require('webpage');
var page = webPage.create();
page.onAlert = function(msg) {
console.log('ALERT: ' + msg);
};
- onClosing
This callback is invoked when the WebPage object is being closed, either via page.close in the PhantomJS outer space or via window.close in the page’s client-side.
It is not invoked when child/descendant pages are being closed unless you also hook them up individually. It takes one argument, closingPage, which is a reference to the page that is closing. Once the onClosing handler has finished executing (returned), the WebPage object closingPage will become invalid.
當網頁對象被關閉時調用這個回調函數。phantomJs的環境中通過page.close和在頁面中通過window.close關閉頁面時觸發。
當子頁面關閉時並不調用此函數,當你單獨打開他們時調用此函數。Closingpage參數指向正在關閉的頁面。
var webPage = require('webpage');
var page = webPage.create();
page.onClosing = function(closingPage) {
console.log('The page is closing! URL: ' + closingPage.url);
};
- onConfirm
This callback is invoked when there is a JavaScript confirm on the web page. The only argument passed to the callback is the string for the message. The return value of the callback handler can be either true or false, which are equivalent to pressing the “OK” or “Cancel” buttons presented in a JavaScript confirm, respectively.
當網頁上有一個javascript 的confirm函數出現時調用此回調。唯一一個被傳送到回調函數的參數是消息的字符串。回調函數的返回值是true或者false,相當於在javascript環境下按下了一個確認按鈕或者取消按鈕。
var webPage = require('webpage');
var page = webPage.create();
page.onConfirm = function(msg) {
console.log('CONFIRM: ' + msg);
return true; // true === pressing the “OK” button, false === pressing the “Cancel” button
};
- onConsoleMessage
This callback is invoked when there is a JavaScript console message on the web page. The callback may accept up to three arguments: the string for the message, the line number, and the source identifier.
當頁面javascript打印消息時調用此函數。這個回調函數接收三個參數(后兩個已經取消):消息(string),行數(number)(已取消),和元標識符(已取消)。
By default, console messages from the web page are not displayed. Using this callback is a typical way to redirect it.
默認的,網頁打印台上顯示的文字不會展示出來。使用這個回調是一種間接方法。
Note: line number and source identifier are not used yet, at least in phantomJS <= 1.8.1. You receive undefined values.
var webPage = require('webpage');
var page = webPage.create();
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
- onError
This callback is invoked when there is a JavaScript execution error. It is a good way to catch problems when evaluating a script in the web page context. The arguments passed to the callback are the error message and the stack trace [as an Array].
當有一個javascript執行錯誤時調用此回調函數。它是當web頁面上下文腳本發生錯誤時候捕獲錯誤的好辦法。傳遞給回掉函數的參數是錯誤消息和堆棧追蹤【數組】
var webPage = require('webpage');
var page = webPage.create();
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'));
};
- onFilePicker
var webPage = require('webpage');
var page = webPage.create();
var system = require('system');
page.onFilePicker = function(oldFile) {
if (system.os.name === 'windows') {
return 'C:\\Windows\\System32\\drivers\\etc\\hosts';
}
return '/etc/hosts';
};
- onInitialized
This callback is invoked after the web page is created but before a URL is loaded. The callback may be used to change global objects.
當頁面創建但是url尚未加載時調用此函數。回調也許會被用來改變全局變量。
var webPage = require('webpage');
var page = webPage.create();
page.onInitialized = function() {
page.evaluate(function() {
document.addEventListener('DOMContentLoaded', function() {
console.log('DOM content has loaded.');
}, false);
});
};
- onLoadFinished
This callback is invoked when the page finishes the loading. It may accept a single argument indicating the page's status: 'success' if no network errors occurred, otherwise 'fail'.
頁面完成加載時調用此回調函數。回調函數接收一個參數表示網頁的狀態:success表示沒有網絡錯誤發生,不然是fail。
Also see page.open for an alternate hook for the onLoadFinished callback.
var webPage = require('webpage');
var page = webPage.create();
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
//Do other things here…
};
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
//Do other things here…
};
- onLoadStarted
This callback is invoked when the page starts the loading. There is no argument passed to the callback.
當頁面開始加載時調用此函數。沒有參數傳遞給回調。
var webPage = require('webpage');
var page = webPage.create();
page.onLoadStarted = function() {
var currentUrl = page.evaluate(function() {
return window.location.href;
});
console.log('Current page ' + currentUrl + ' will gone…');
console.log('Now loading a new page…');
};
- onNavigationRequested
By implementing this callback, you will be notified when a navigation event happens and know if it will be blocked (by page.navigationLocked).
通過調用這一個回調函數,當導航事件發生時你將會得到通知,並且知道這個是否會被阻塞(通過page.navigationLocked)。
Arguments
url : The target URL of this navigation event
導航事件的目標URL
type : Possible values include: 'Undefined', 'LinkClicked', 'FormSubmitted', 'BackOrForward', 'Reload', 'FormResubmitted', 'Other'
可能值:'Undefined', 'LinkClicked', 'FormSubmitted', 'BackOrForward', 'Reload', 'FormResubmitted', 'Other'
willNavigate : true if navigation will happen, false if it is locked (by page.navigationLocked)
true當導航發生,被(通過page.navigationLocked)鎖住時是false
main : true if this event comes from the main frame, false if it comes from an iframe of some other sub-frame.
當這個事件來自主頁面時為true,否則是false。
var webPage = require('webpage');
var page = webPage.create();
page.onNavigationRequested = function(url, type, willNavigate, main) {
console.log('Trying to navigate to: ' + url);
console.log('Caused by: ' + type);
console.log('Will actually navigate: ' + willNavigate);
console.log('Sent from the page\'s main frame: ' + main);
};
- onPageCreated
This callback is invoked when a new child window (but not deeper descendant windows) is created by the page, e.g. using window.open.
頁面創建新的子窗口的時候調用此回調。例如使用window.open
In the PhantomJS outer space, this WebPage object will not yet have called its own page.open method yet and thus does not yet know its requested URL (page.url).
在phantomjs的外部環境 這個webpage對象尚未訪問自己的page.open方法,所以它還不知道請求的URL。
Therefore, the most common purpose for utilizing a page.onPageCreated callback is to decorate the page (e.g. hook up callbacks, etc.).
因此,page.onpageCreated最常見的用途是裝飾頁面。
var webPage = require('webpage');
var page = webPage.create();
page.onPageCreated = function(newPage) {
console.log('A new child page was created! Its requested URL is not yet available, though.');
// Decorate
newPage.onClosing = function(closingPage) {
console.log('A child page is closing: ' + closingPage.url);
};
};
- onPrompt
This callback is invoked when there is a JavaScript prompt on the web page. The arguments passed to the callback are the string for the message (msg) and the default value (defaultVal) for the prompt answer. The return value of the callback handler should be a string.
當網頁端有javascript 的prompt()時調用此函數。傳給回調函數的參數是msg消息和prompt()回答的默認值。返回值應該是string格式。
var webPage = require('webpage');
var page = webPage.create();
page.onPrompt = function(msg, defaultVal) {
if (msg === "What's your name?") {
return 'PhantomJS';
}
return defaultVal;
};
- onResourceError
This callback is invoked when a web page was unable to load resource. The only argument to the callback is the resourceError metadata object.
當一個頁面無法加載資源時調用此函數。傳給回掉函數的參數是resourceerror元數據對象。
The resourceError metadata object contains these properties:
resourceError元數據含有下列屬性:
id : the number of the request
url : the resource url
errorCode : the error code
errorString : the error description
var webPage = require('webpage');
var page = webPage.create();
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
- onResourceReceived
This callback is invoked when a resource requested by the page is received. The only argument to the callback is the response metadata object.
當頁面請求的資源收到時調用回調。傳給回調的參數是resourceerror元數據對象。
If the resource is large and sent by the server in multiple chunks, onResourceReceived will be invoked for every chunk received by PhantomJS.
當資源過大時服務端將會分成多塊傳送資源。onResourceReceived()將會在每一塊數據被PhantomJs接收時調用。
The response metadata object contains these properties:
id : the number of the requested resource
url : the URL of the requested resource
time : Date object containing the date of the response
headers : list of http headers
bodySize : size of the received content decompressed (entire content or chunk content)
contentType : the content type if specified
redirectURL : if there is a redirection, the redirected URL
stage : "start", "end" (FIXME: other value for intermediate chunk?)
status : http status code. ex: 200
statusText : http status text. ex: OK
var webPage = require('webpage');
var page = webPage.create();
page.onResourceReceived = function(response) {
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};
- onResourceRequested
This callback is invoked when the page requests a resource. The first argument to the callback is the requestData metadata object. The second argument is the networkRequest object itself.
頁面請求資源時調用此函數。第一個參數是resourceerror元數據對象,第二個參數是networkRequest對象本身。
The requestData metadata object contains these properties:
id : the number of the requested resource
method : http method
url : the URL of the requested resource
time : Date object containing the date of the request
headers : list of http headers
The networkRequest object contains these functions:
abort() : aborts the current network request. Aborting the current network request will invoke onResourceError callback.
終止當前的網絡請求,將會調用回調函數onResourceError。
changeUrl(newUrl) : changes the current URL of the network request. By calling networkRequest.changeUrl(newUrl), we can change the request url to the new url. This is an excellent and only way to provide alternative implementation of a remote resource. (see Example-2)
改變當前網絡請求的URL。這是提供一個遠程資源替代實現的唯一途徑。
setHeader(key, value)
Examples
1 Example-1
var webPage = require('webpage');
var page = webPage.create();
page.onResourceRequested = function(requestData, networkRequest) {
console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};
2 Example-2
Provide an alternative implementation of a remote javascript.
var webPage = require('webpage');
var page = webPage.create();
page.onResourceRequested = function(requestData, networkRequest) {
var match = requestData.url.match(/wordfamily.js/g);
if (match != null) {
console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
// newWordFamily.js is an alternative implementation of wordFamily.js
// and is available in local path
networkRequest.changeUrl('newWordFamily.js');
}
};
- onResourceTimeout
This callback is invoked when a resource requested by the page timeout according to settings.resourceTimeout. The only argument to the callback is the request metadata object.
頁面請求的資源超時調用此函數。傳給回調函數的唯一參數是請求元數據對象。
The request metadata object contains these properties:
id: the number of the requested resource
method: http method
url: the URL of the requested resource
time: Date object containing the date of the request
headers: list of http headers
errorCode: the error code of the error
errorString: text message of the error
var webPage = require('webpage');
var page = webPage.create();
page.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};
- onUrlChanged
This callback is invoked when the URL changes, e.g. as it navigates away from the current URL. The only argument to the callback is the new targetUrl string.
當url變化(比如它被導航離開當前頁面的url時)調用此回調函數。傳給回調函數的唯一參數是新的targetURl字符串。
To retrieve the old URL, use the onLoadStarted callback.
使用onLoadStarted回調函數以取回老的url。
var webPage = require('webpage');
var page = webPage.create();
page.onUrlChanged = function(targetUrl) {
console.log('New URL: ' + targetUrl);
};
Child Process Module
The child_process module allows you to invoke subprocesses and communicate with them via stdin / stdout / stderr. This is useful for tasks such as printing, sending mail, or invoking scripts or programs written in another language (not Javascript).
子進程模塊允許你調用子進程並且與之通過 stdin/stdiout/stderr進行交流。這對於打印或者發送郵件的任務來說很有用。或者調用其它語言編寫的腳本或者程序。
To start using, you must require a reference to the child_process module:
var process = require("child_process")
var spawn = process.spawn
var execFile = process.execFile
var child = spawn("ls", ["-lF", "/rooot"])
child.stdout.on("data", function (data) {
console.log("spawnSTDOUT:", JSON.stringify(data))
});
child.stderr.on("data", function (data) {
console.log("spawnSTDERR:", JSON.stringify(data))
});
child.on("exit", function (code) {
console.log("spawnEXIT:", code)
});
//child.kill("SIGKILL")
execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) {
console.log("execFileSTDOUT:", JSON.stringify(stdout))
console.log("execFileSTDERR:", JSON.stringify(stderr))
});
- Spawn
var page = require('page').create();
var spawn = require('child_process').spawn;
page.open('http://google.com', function(status){
if( status == 'success' ) {
page.render('/tmp/google-snapshot.jpg');
spawn('/usr/bin/sensible-browser', 'file:///tmp/google-snapshot.jpg');
phantom.exit();
}
});
File System Module
A set of API functions is available to access files and directories, modeled after the CommonJS Filesystem proposal.
一租可以訪問文件和目錄的文件系統方案的函數,來自CommonJS。
To start using, you must require a reference to the fs module:
var fs = require('fs');
Stream objects are returned from the fs.open method.
fs.open方法返回一個流對象。
Separator
workingDirectory
var fs = require("fs");
console.log(fs.workingDirectory);// return "F:/liu/study/phantomjs/examples"(the directory the file exsit)
- "absolute(string)" (string)
Gets the absolute path to where the phantomjs is been run from. If you use relative addresses then you can get the actual address of directories above and below the phantomjs program.
得到phantomjs運行的絕對路徑。如果你使用相對路徑,那么你可以得到目錄的實際路徑
var fs = require("fs");
// Print the directory from which phantomjs was run:
var cwd = fs.absolute(".");
console.log(cwd);
// The parent directory:
var parent = fs.absolute("../");
console.log(parent);
// Absolute path of a child directory:
var kid = fs.absolute("/below");
console.log(kid);
- changeWorkingDirectory (string) (BOOL)
This allows you to change the workingDirectory and returns true if the change happened else false
改變工作目錄,成功返回true,錯誤返回false
var fs = require("fs");
console.log(fs.workingDirectory); //prints the location where phantomjs is running
fs.changeWorkingDirectory("C:\\");
console.log(fs.workingDirectory); //prints C:/
- copy(string source, string destination)"
This will try to copy a file from one path to another.
這個函數嘗試將一個文件拷貝到另外一個文件。
Parameter 1 is the source file and parameter 2 is the destination path with the file name.
參數1是源文件,參數2是帶目標地址的文件名。
If the source file can"t be found then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution.
如果源文件無法被發現,將會拋出一個錯誤"無法復制文件到目的地"並且暫停執行。
If the destination can not be created then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution. It will not overwrite existing files.
如果目的地文件無法被創建,將會拋出一個錯誤"無法復制文件到目的地"並且暫停執行。這個函數無法重寫已經存在的文件。
var fs = require("fs");
fs.copy("A.txt", "folder/A.txt");
- copyTree (string source, string destination)
This will try to copy a directory tree from one path to another.
這個函數將嘗試拷貝一個目錄結構到另外一個地方。
Parameter 1 is the source folder tree and parameter 2 is the destination path.
參數1是源文件樹參數2是目的目錄。
If the destination folder doesn"t exist, it will be created. Then, every file and folder will be copied in the destination folder. Folders will be recursively copied.
如果目的文件夾不存在,其將被創建。每一個文件和每一個文件夾都會被拷貝到目的文件夾。文件夾會被遞歸地拷貝。
If one of the files or folders fails to be copied or created, it will throw a "Unable to copy directory tree SOURCE at DESTINATION" and hang execution.
如果其中一個文件無法被拷貝或者創建,其將會拋出錯誤"Unable to copy directory tree SOURCE at DESTINATION"並且暫停執行。
var fs = require("fs");
fs.copyTree("sourceFolder/", "destinationFolder/");
phantom.exit();
- exists(string) (BOOL)
This will return true if the file exists, otherwise it will return false. This follows symlinks to determinate if a file exists.
返回文件是否存在。存在true,否則false。
var fs = require("fs");
var path = "output.txt";
if (!fs.exists(path))
fs.write("Hello World", path, "w");
phantom.exit();
var fs = require("fs");
var path = "/Full/Path/To/test.txt";
if (fs.exists(path))
console.log("'"+path+"' exists.");
else
console.log("'"+path+"' doesn\'t exist.");
phantom.exit();
- "isAbsolute(string)" (BOOL)
This will return true if the file path is absolute, otherwise it will return false if the path is relative.
如果文件路徑是絕對路徑返回true,否則返回false
var fs = require("fs");
var path = "/Full/Path/To/test.txt";
// isAbsolute(path) returns true if the specified path is an absolute path.
if (fs.isAbsolute(path))
console.log("'"+path+"' is an absolute path.");
else
console.log("'"+path+"' is NOT an absolute path.");
phantom.exit();
- isDirectory(string)" (BOOL)
var fs = require("fs");
var path = "/etc/";
// Get a list all files in directory
var list = fs.list(path);
// Cycle through the list
for(var x = 0; x < list.length; x++){
// Note: If you didn"t end path with a slash, you need to do so here.
var file = path + list[x];
if(fs.isDirectory(file)){
// Do something
}
}
phantom.exit();
- isExecutable(string)" (BOOL)
var fs = require("fs");
var path = "/Full/Path/To/exec";
if (fs.isExecutable(path))
console.log("'"+path+"' is executable.");
else
console.log("'"+path+"' is NOT executable.");
phantom.exit();
- "isFile(string)" (BOOL)
var fs = require("fs");
var path = "/etc/";
// Get a list all files in directory
var list = fs.list(path);
// Cycle through the list
for(var x = 0; x < list.length; x++){
// Note: If you didn"t end path with a slash, you need to do so here.
var file = path + list[x];
if(fs.isFile(file)){
// Do something
}
};
phantom.exit();
- "isLink(string)" (BOOL)
This will return true if the file path is a symlink (or a shortcut on Windows), otherwise it will return false.
判斷是否是個symlink(或者在windows下是個快捷方式)。
Be aware that Unix-like systems and Windows systems don"t manage symlinks and shortcuts the same way.
注意unix類系統和windows系統有不同的方式。
var fs = require("fs");
var path = "/Full/Path/To/file";
if (fs.isLink(path))
console.log("'"+path+"' is a link.");
else
console.log("'"+path+"' is an absolute path");
phantom.exit();
- "isReadable(string)" (BOOL)
This will return true if the file is readable, otherwise it will return false.
是否可讀。
var fs = require("fs");
var path = "/Full/Path/To/file";
if (fs.isReadable(path)) {
console.log("'"+path+"' is readable.");
// Open the file
var otherFile = fs.open(path, {
mode: "r" //Read mode
});
// Do something with the opened file
}
else
console.log("'"+path+"' is NOT readable.");
phantom.exit();
- isWritable(string)" (BOOL)
This will return true if the file is writable, otherwise it will return false.
是否可寫。
var fs = require("fs");
var path = "/Full/Path/To/file";
if (fs.isWritable(path)) {
console.log("'"+path+"' is writable.");
var content = "Hello World!";
fs.write(path, content, "w");
}
else
console.log("'"+path+"' is NOT writable.");
phantom.exit();
- list(string)" (Array)
List the contents of a directory on the file system as simple array. This will returns an empty array if the directory is unreadable or doesn"t exist.
列出一個目錄中的所有內容。如果目錄無法訪問或者不存在返回一個空數列。
// EXAMPLE: Show a list of files in a specific directory as a fully qualified path:
var fs = require("fs");
var path = "/etc/";
// Get a list all files in directory
var list = fs.list(path);
// Cycle through the list
for(var x = 0; x < list.length; x++){
// Note: If you didn"t end path with a slash, you need to do so here.
var file = path + list[x];
if(fs.isFile(file)){
// Do something
}
}
phantom.exit();
- "makeDirectory(string)" (BOOL)
Creates a directory at the given path.
創造文件夾。成功true,失敗false。如果已經存在文件夾,返回false
This will returns true if the creation was successful, otherwise false. If the directory already exists, it will returns false.
var fs = require("fs");
var path = "/Full/Path/To/Test/";
if(fs.makeDirectory(test))
console.log("'"+path+"' was created.");
else
console.log("'"+path+"' is NOT created.");
phantom.exit();
- makeTree(string)" (BOOL)
Creates all necessary directories to be able to create the directory.
This will returns true if the creation was successful, otherwise false. If the directory already exists, it will returns true.
var fs = require("fs");
var path = "/Full/Path/To/Test/";
if(fs.makeDirectory(test))
console.log("'"+path+"' was created.");
else
console.log("'"+path+"' is NOT created.");
phantom.exit();
- move(string source, string destination)"
This will try to move a file from one path to another.
移動文件。
Parameter 1 is the source file and parameter 2 is the destination path with the file name.
參數一為源文件,參數二位目標文件地址。
If the source file can"t be found then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution.
If the destination can not be created then it will throw a "Unable to copy file SOURCE at DESTINATION" and hang execution. It will not overwrite existing files.
If the source file can not be deleted then it will throw a "Unable to delete file SOURCE" and hang execution
var fs = require("fs");
fs.move("A.txt", "folder/A.txt");
- "open(string path, string/Object parameters)" (Stream)
打開文件。
The parameter object can contain :
mode: Open Mode. A string made of "r", "w", "a/+", "b" characters.打開方式
charset : An IANA, case insensitive, charset name.字符集名稱
Stream objects are returned from the fs.open method. Don"t forget to close the stream.
When errors occur during a call, it will throw a "Unable to open file PATH" and hang execution.
var fs = require("fs");
var file = fs.open("/path/to/file", "r"); //Open Mode. A string made of "r", "w", "a/+", "b" characters.
var otherFile = fs.open("/path/to/otherFile", {
mode: "r" //(see Open Mode above)
});
var yetAnotherFile = fs.open("/path/to/yetAnotherFile", {
mode: "w", //(see Open Mode above)
charset: "US-ASCII" //An IANA, case insensitive, charset name.
});
file.close();
otherFile.close();
yetAnotherFile.close();
- "read(string path, string/Object parameters)" (string)
The parameter object can contain :
mode: Open Mode. A string made of "r", "w", "a/+", "b" characters.
charset : An IANA, case insensitive, charset name.
Opens the path and returns the entire contents as a string.
When errors occur during a call, it will throw a "Unable to open file PATH" and hang execution.
Note that the behavior of this method is closely related to fs.open.
var fs = require("fs");
var content = fs.read("file.txt");
console.log("read data:", content);
phantom.exit();
- "readLink(string)" (string)
This will return the absolute path of a file or folder pointed by a symlink (or shortcut on Windows). If the path is not an symlink or shortcut, it will return an empty string.
返回一個文件或者文件夾的軟連接的絕對路徑(在windows下是快捷方式)。如果路徑不是一個軟鏈接或者快捷方式,將返回一個空字符串
var fs = require("fs");
var path = "/Full/Path/To/test.txt";
if (fs.isLink(path)) {
var absolute = fs.readLink(path);
console.log("The absolute path of " + path + " is " + absolute);
}
else
console.log("'"+path+"' is an absolute path");
phantom.exit();
- remove(string)"
This will try to delete the specified file.
刪除指定文件
When errors occur during a call, it will throw a "Unable to remove file PATH" and hang execution.
var fs = require("fs");
var toDelete = "someFile.txt";
fs.removeDirectory(toDelete);
phantom.exit();
- removeDirectory(string)"
This will try to delete the specified folder.
刪除文件夾
The directory needs to be empty to be removed with this method. To delete an non empty folder, fs.removeTree should be used.
When errors occur during a call, it will throw a "Unable to remove directory PATH" and hang execution.
var fs = require("fs");
var toDelete = "someFolder";
// Test if the folder is empty before deleting it
if(fs.list(toDelete).length === 0)
fs.removeDirectory(toDelete);
phantom.exit();
- "removeTree(string)"
刪除樹
This will try to delete every file and folder in the specified folder and, finally, delete the folder itself.
When errors occur during a call, it will throw a "Unable to remove directory tree PATH" and hang execution.
var fs = require("fs");
var toDelete = "someFolder";
fs.makeDirectory(toDelete);
fs.touch(toDelete + "/test.txt");
// It will delete the "someFolder" and the "test.txt" in it
fs.removeDirectory(toDelete);
phantom.exit();
- size(string)" (int)
This will return the specified file size in bytes, if it exists.
返回指定文件的長度
When errors occur during a call, it will throw a "Unable to read file PATH size" and hang execution.
var fs = require("fs");
var path = "someFolder/";
// Go through every element in the folder
fs.list(path).forEach(function(file) {
file = path + file;
// Remove every file that are larger than 100 bytes
if(fs.isFile(file) && fs.size(file) > 100)
fs.remove(file);
});
phantom.exit();
- touch(string)"
This will try to create an empty file at the specified path.
連接一個特殊路徑的空文件。
When errors occur during a call, it will throw a "Unable to open file PATH" and hang execution. The call won"t throw an error if the file already exists.
Note that the behavior of this method is closely related to fs.open and Stream.write.
var fs = require("fs");
var path = "test.txt";
// Creates an empty file
fs.touch(path);
var text = fs.read(path);
// If the test.txt didn"t already exist, this will print an empty string
console.log(text);
phantom.exit();
- "write(string source, string content, string/Object parameters)"
寫入文件
The parameter object can contain :
mode: Open Mode. A string made of "r", "w", "a/+", "b" characters.
charset : An IANA, case insensitive, charset name.
If the source file can"t be openend then it will throw a "Unable to open file SOURCE" and hang execution.
var fs = require("fs");
var path = "output.txt";
var content = "Hello World!";
fs.write(path, content, "w");
phantom.exit();
System Module
To start using, you must require a reference to the system module:
var system = require('system');
- system.args {String[]}
Queries and returns a list of the command-line arguments. The first one is always the script name, which is then followed by the subsequent arguments.
查詢並返回一個列表的命令行參數。第一個是腳本名稱,其后是后續參數。
The following example prints all of the command-line arguments:
var system = require('system');
var args = system.args;
if (args.length === 1) {
console.log('Try to pass some arguments when invoking this script!');
} else {
args.forEach(function(arg, i) {
console.log(i + ': ' + arg);
});
};
- system.env {Object}
Queries and returns a list of key-value pairs representing the environment variables.
查詢並返回一個鍵-值對列表表示環境變量
var system = require('system');
var env = system.env;
Object.keys(env).forEach(function(key) {
console.log(key + '=' + env[key]);
});
- Os
Read-only. An object providing information about the operating system, including architecture, name, and version.
只讀,一個提供有關操作系統的對象。包括系統位數,名稱和版本。
var system = require('system');
var os = system.os;
console.log(os.architecture); // '32bit'
console.log(os.name); // 'windows'
console.log(os.version); // '7'
phantom.exit();
- system.pid {Number}
Introduced: PhantomJS 1.8 Read-only. The PID (Process ID) for the currently executing PhantomJS process.
1.8引入,只讀。正在執行的phantomjs程序的 PID(程序ID)
- system.platform {String}
Read-only. The name of the platform, for which the value is always 'phantomjs'.
只讀。平台的名稱,總是"phantomjs"
var system = require('system');
console.log(system.platform); // 'phantomjs'
phantom.exit();
Web Server Module
- Stability: EXPERIMENTAL
穩定性:試驗性質
Using an embedded web server module called Mongoose, PhantomJS script can start a web server. This is intended for ease of communication between PhantomJS scripts and the outside world and is not recommended for use as a general production server. There is currently a limit of 10 concurrent requests; any other requests will be queued up.
通過使用一個名為"mongoose"嵌入的web服務模塊,phantomJs腳本可以開啟一個web服務器。這個是 phantomjs腳本和外界進行交流的一種方式,但並不推薦用於一般的生產環境。目前有10個並發請求限制,其他任何請求都將排隊。
var webserver = require('webserver');
- port
The port on which the server listen requests (read only)
服務端監聽請求的端口號(只讀。)
Close
Listen
request argument
The request object passed to the callback function may contain the following properties:
請求對象傳遞給回調函數可能包含以下屬性:
method: Defines the request method ('GET', 'POST', etc.)
url: The path part and query string part (if any) of the request URL
httpVersion: The actual HTTP version
headers: All of the HTTP headers as key-value pairs
post: The request body (only for 'POST' and 'PUT' method requests)
postRaw:
If the Content-Type header is set to 'application/x-www-form-urlencoded' (the default for form submissions), the original contents of post will be stored in this extra property (postRaw) and then post will be automatically updated with a URL-decoded version of the data.
如果Content-Type header設置成"application/x-www-form-urlencoded"(表單默認提交),原先提交的內容將被存在額外的屬性(postRow)中,並且原先的post將會自動被包括URL-decoded版本的數據重寫。
- response argument
The response object should be used to create the response using the following properties and functions:
響應對象應該以以下屬性和函數創建:
headers:
Stores all HTTP headers as key-value pairs. These must be set BEFORE calling write for the first time.
以鍵值對存放一切http header。必須在write被第一次使用之前設置。
setHeader(name, value): sets a specific header
header(name): returns the value of the given header
statusCode: Sets the returned HTTP status code.
setEncoding(encoding): Indicate the encoding to use to convert data given to write(). By default data will be converted to UTF-8. Indicate "binary" if data is a binary string.
write(data): Sends a chunk for the response body. Can be called multiple times.
writeHead(statusCode, headers): Sends a response header to the request. The status code is a 3-digit HTTP status code (e.g. 404). The last argument, headers, are the response headers. Optionally one can give a human-readable headers collection as the second argument.
- close(): Closes the HTTP connection.
To avoid the client detecting a connection drop, remember to use write() at least once. Sending an empty string (i.e. response.write(")) would be enough if the only aim is, for example, to return an HTTP status code of 200 ("OK").
為避免客戶檢測到一個鏈接中斷,記得使用至少一次write()發送一個空字符串。這種情況適用於目標僅僅是返回 一個http狀態嗎200的情況。
- closeGracefully(): same as close(), but ensures response headers have been sent first (and do at least a response.write("))
自動地在關閉前發送一個空字符串。
