phantomjs 是一個具備webkit內核的無界面瀏覽器,有不少強大的功能,作爬蟲也是很簡單的(固然是簡單的爬蟲),web
這是一個爬取搜狗相關搜索的例子,能夠多加點東西,或者改爲爬取其餘網頁的爬蟲:瀏覽器
var page = require('webpage').create(); var system = require('system'); if (system.args.length != 2) { console.log("input keyword to search!"); } else { // 設置編碼通常使用System不會出錯 utf8反而不給力 phantom.outputEncoding = 'System'; // 經過命令行讀取搜索參數 address = encodeURI('http://www.sogou.com/web?query=' + system.args[1]); console.log(address); try { var openbaidu = page.open(address, function(status) { var title = page.evaluate(function() { // WebKit接口 var res = document.querySelectorAll('#hint_container a'); var res_text = ''; for (i = 0; i < res.length; i++) { res_text += res[i].innerHTML + "\n"; } return res_text; }); console.log(title); }); openbaidu; } catch (e) { console.log(e); } }
使用實例:ui