javascript
CasperJS基于PhantomJS抓取页面
CasperJS基于PhantomJS抓取頁面
Casperjs是基于Phantomjs的,而Phantom JS是一個服務器端的 JavaScript API 的 WebKit。
CasperJS是一個開源的,用JavaScript編寫的,基于PhantomJS的導航腳本和測試工具 ,它簡化了定義一個完成的導航操作所需的步驟,還提供了很有用的函數封裝,方法,和語法糖,它可以完成下面這些常見任務:
定義 & 排序瀏覽器導航步驟
填充 & 提交表單
點擊 & 跟蹤鏈接
捕獲網頁截圖 (還可以截取某一區域)
在遠程DOM上進行斷言測試
記錄事件
下載資源,包括二進制文件
編寫功能測試套件,結果保存為JUnit XML文件
抓取網頁內容
CasperJS,基于PhantomJS的工具包 - 紫云飛 - 博客園
http://www.cnblogs.com/ziyunfei/archive/2012/09/27/2706254.html
后臺連接網站和頁面交互一下子變得異常簡單起來。尤其是對需要登錄才可進行的網頁操作。
登錄滬江英語自動打卡
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false,
loadPlugins: true,
userAgent: 'Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0'
}
});
//phantom.outputEncoding="gbk";
casper.options.viewportSize = {width: 1680, height: 924};
casper.start('http://bulo.hujiang.com/app/login?source=nbulo&returnurl=/home/');
casper.waitForSelector("form#myform input[name='txtUsername']",
function success() {
this.test.assertExists("form input[name='txtUsername']");
this.fill("form",{
'txtUsername':'shixiaobao17',
'txtPassword':'×××××your password*****'
},false);
this.click("input#btnLogin");
},
function fail() {
this.test.assertExists("form input[name='txtUsername']");
});
casper.waitFor(function check() {
return this.getCurrentUrl().indexOf("bulo.hujiang.com/home")>-1;
}, function then() {
console.log("登錄成功!!!!!!!!!!!!");
}).then(function(){
console.log("執行登錄后的其它操作!!!!!!!!!!!!");
if(this.exists("#btn_card_do")){
this.click("#btn_card_do");
this.waitForSelector("#my_hb_btn",function success(){
console.log("打卡成功!");
},function fail(){
console.log("打卡失敗!");
});
}else{
console.log("今天已經打過卡啦!");
}
});
casper.run(function() {this.test.renderResults(true);});
總結
以上是生活随笔為你收集整理的CasperJS基于PhantomJS抓取页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring实战6-利用Spring和J
- 下一篇: 转 Django+Bootstrap练习