標籤:失敗 phantomjs object 示範 org bpa mac 使用者目錄 建立
# 小文筆記 - phantomjs2017-05-13## 第一節:安裝``` txt Windows安裝: 下載解壓檔案 我的電腦 -> 屬性 -> 進階系統設定 點擊環境變數 在系統變數視窗中,找到Path變數,選中然後選擇編輯 建立後,添加新的環境變數(phantomjs.exe)所在路徑 在cmd中,運行phantomjs Mac安裝: 下載解壓檔案 在使用者目錄下查看ls -a查看全部檔案 開啟.bash_profile檔案 加入export PATH=$PATH:/Users/suoyong/phantomjs-2.1.1-macosx/bin 在終端中輸入phantomjs運行``` `phantomjs -v` 查看版本。`phantomjs a.js` 運行 `a.js` 檔案。`phantomjs` 進入程式, `console.log(1)` ,運行 js 代碼。`phantom.exit()` 退出.## 第二節:核心模組``` txt Web page 模組 檔案模組 系統模組 子進程模組 網路服務模組 網站 phantomjs.org/api/``` 使用 `require(‘模組名‘)` 引用模組。a.js``` js var sys = require(‘system‘); console.log(sys.args); phantom.exit();``` 運行,a.js 是第一個參數,hi 是第二個參數。``` bat phantomjs a.js hi a.js,hi``` ## 第三節:WebPage對象(一)``` txt 建立WebPage對象:create() 開啟網址:page.open 在evaluate方法中操作頁面(DOM,JSON,Canvas等) 把當前頁面儲存成圖片:page.render() .create() 建立 webPage 對象執行個體。 .open() 開啟網址。 .evaluate() 在這個裡面才能操作 web 介面中的內容。``` 開啟百度首網頁標題``` js var webpage = require(‘webpage‘); var page = webpage.create(); page.open(‘https://www.baidu.com‘,function(){ // 開啟後做的事 var title = page.evaluate(function(){ // 操作頁面 return document.title }); console.log(title); phantom.exit(1); // 1 成功 0失敗 })``` 開啟對象``` js var webpage = require(‘webpage‘); var page = webpage.create(); page.open(‘https://www.baidu.com‘,function(){ // 開啟後做的事 var title = page.evaluate(function(){ // 操作頁面 return document.getElementById(‘lg‘); }); // console.log(title) // [object Object] ,他其他是一個對象 // console.dir(title) // [object Object] ,對象 console.log(JSON.stringify(title)); // 所以需要轉換一下 phantom.exit(1); // 1 成功 0失敗 })``` ## 第三節:WebPage對象(二)不能在 evaluate 中直接使用 console.log 顯示 dom 資訊,因為為了不影響其他頁面正常運行, evaluate 是運行在沙箱中的,他沒有 console.log 。``` js var webpage = require(‘webpage‘); var page = webpage.create(); page.open(‘https://www.baidu.com‘,function(){ page.evaluate(function(){ console.log(document.getElementById(‘lg‘)); // 不會有 console.log() 輸出。 }); phantom.exit(1); })``` 解決方案:為 webpage 對象添加回呼函數。``` js var webpage = require(‘webpage‘); var page = webpage.create(); page.onConsoleMessage = function(msg){ console.log(msg); } page.open(‘http://www.intalesson.com/‘,function(){ page.evaluate(function(){ console.log(document.title); }); phantom.exit(1); })``` ### 傳送參數如下例添加一個 arg 參數,值為 ‘hi‘ 。``` js var webpage = require(‘webpage‘); var page = webpage.create(); page.onConsoleMessage = function(msg){ console.log(msg); } page.open(‘http://www.intalesson.com/‘,function(){ var title = page.evaluate(function(arg){ console.log(arg); },‘hi‘); phantom.exit(1); })``` ### 設定 user agent`page.settings.userAgent = ‘要設定的使用者代理程式‘`## 第五節:提交表單- `page.onLoadFinished = function(){}` 當頁面載入完後執行的函數。- `Dom.submit()` 提交。- `模仿點擊事件` 提交。示範登入智聯招聘並儲存。``` js var webpage = require(‘webpage‘); var page = webpage.create(); page.open(‘http://www.zhaopin.com/‘,function(){ page.evaluate(function(){ var user = document.getElementById(‘loginname‘); var pass = document.getElementById(‘password‘); user.value = ‘使用者名稱‘; pass.value = ‘密碼‘; var submit = document.querySelector(‘.logbtn button‘); var evt = document.createEvent(‘MouseEvents‘); // 建立一個滑鼠事件 evt.initMouseEvent(‘click‘); // 初始化一個滑鼠點擊事件 submit.dispatchEvent(evt); // 使用事件,提交表單 }) page.onLoadFinished = function(status){ if(status == ‘success‘){ // 檢查頁面是否載入完畢 page.render(‘1.png‘); // 把頁面儲存圖片 phantom.exit(1); } } })``` ## 第六節:操作Cookie- cookies 查看- addCookie 設定 當設定 cookie 時一定要設定 domain name value 三個值。``` js phantom.addCookie({‘domain‘:‘.baidu.com‘,‘name‘:‘xw‘,‘value‘:‘1‘}); console.log(JSON.stringify(phantom.cookies)); phantom.exit(1);``` ## 第七節:CasperJSCasperJS 擴充自 phantomJS ,可更簡單的操作頁面元素。phantomjs 不是 nodejs 的模組,但 casperjs 可使用 npm 安裝。- `casper.start() = page.open()`- `echo() = console.log()`phantomjs 像 js , casperjs 像 jq 。- 安裝 casperjs `npm install -g casperjs`## 第八節:步進式指令碼語言## 小文注亂碼問題: http://blog.csdn.net/kaosini/article/details/47252457- 方法一:在 js 檔案中添加 `phantom.outputEncoding="gbk"` 可解決亂碼。- 方法二:`phantomjs --output-encoding=gbk a.js`
原文連結: http://www.cnblogs.com/daysme/p/6850956.html
小文筆記 - phantomjs