`

casperjs爬虫

 
阅读更多
主要是抓一些网站的数据特别对于mechanize无法抓取的js产生的数据
(1)casperjs是在phatomjs基础上来的所以安装casperjs必须先安装phatomjs
   http://casperjs.org/   http://phantomjs.org/ 都有
(2)casperjs是按步来的,所以start   run  then这三个方法中前两者是必须有的
(3)evaluate方法是document对外的接口,里面不能有自己定义的一些方法使用,一般都是设计到元素选择器查找之类的方法__utils__.findAll("div.rank-s ul.rank1-body li")
(4)会看到很多与then配合使用的方法,加then之后表示必须等待上一个动作完成才会运行此方法这步。
(5) captrue这个方法会把当前页面拍照,然后保存起来有时候很有用  this.wait(20000,function init(){
        // this.capture(work_path+"/"+p+"child"+".jpg")
});
(6)cookie读取与保存
    phantom.cookiesEnabled = true;
//设置访问页面的cookie
function updateCookie() {
    var cookiefile = work_path+"/cookie.txt";
    var cookies = JSON.parse(fs.read(cookiefile));
    for (var i = 0; i < cookies.length; i++) {
       phantom.addCookie(cookies[i]);
    }
}
   保存
   phantom.cookiesEnabled = true;
casper.then(function(){
    this.wait(20000,function wait_submit(){
    //--验证是否登陆成功
    this.capture(work_path+"/is_login.jpg");
    fs.write(work_path+"/cookie.txt",JSON.stringify(phantom.cookies),777);
    });
});

自己项目中用到的
  1)casper = require("casper").create();
  2)casper.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
3)casper.start("http://xxxx.com/");
4)casper.then(function(){
//模拟按键
this.sendKeys('#username','youname',{reset: true});
this.sendKeys('#password','youpassword',{keepFocus: true});
});
5)casper.wait(800,function init(){
    this.capture(work_path+"/yzm.png");
    console.log("验证码已拍照完成,等待30s")
});可以看看是不是已经登录,通过截取画面
6)日志文件输出在控制台,也可以>导引到其它文件里面
    casper.on('remote.message', function(message) {
        console.log(message);
   });
7)其它的都是一个then方法按步骤获取页面数据
var infoss = casper.evaluate(function(){
      var info = {
                    "name": name,
                };
                infos.push(info);
        return infos;
    })
分享到:
评论

相关推荐

    casperjs-1.1.3.zip

    爬虫,网页javascript

    casper-crowly:基于 casper.js 的网络爬虫

    如何跑步克隆存储库后,运行npm install 如果你在全局安装了 casperjs,你可以简单地运行 - casperjs --ssl-protocol=any index.js &lt;url&gt; 或使用以下本地安装的 casperjs 运行它 - ./node_modules/.bin/casperjs --...

    casperjs +phantomjs 爬虫抓包插件

    利用casperjs +phantomjs实现登录抓取网页 并且截图;下载后解压后,在环境变量path中配置解压后bin目录的地址,如:E:\Program Files\casperjs\bin;E:\Program Files\phantomjs\bin

    Linux版本casperjs,抓取js加载的页面

    网络爬虫,JS加载的页面的抓取,casperjs的使用,压缩包有代码示例。java爬虫项目,抓取动态页面

    casperjs抓取简单实例

    casperjs抓取简单实例, 根据输入的内容,获取百度百科的内容。运行可用。

    一个动态web爬虫_python_JavaScript_代码_下载

    基于CasperJS和PhantomJS,可以自动渲染网页、动态解析js,支持ajax和各类前端交互。 代码基于phantomjs爬虫小记 by wils0n ,在tuicool上也有这篇文章http://www.tuicool.com/articles/JbEfIvV , 原作者的代码在...

    casperjs_biztest.rar

    CasperJS是一个开源的导航脚本处理和测试工具,基于PhantomJS。CasperJS简化了页面间导航的定义过程,提供了处理常见任务的实用的高级函数、方法和语法糖。

    FriendlyGhost:用于抓取电子邮件地址的 Casperjs 驱动的网络爬虫

    Friendly Ghost依赖于 Casperjs 和 Phantomjs。 安装这些的最好方法是通过终端通过 。 -g标志使它们可以在系统范围内访问。 sudo npm install -g phantomjs && sudo npm -g install casperjs 其他安装选项包括和 ...

    在Python中使用CasperJS获取JS渲染生成的HTML内容的教程

    长期以来,爬虫抓取 客户端javascript渲染生成的html页面 都极为 困难, Java里面有 HtmlUnit, 而Python里,我们可以使用独立的跨平台的CasperJS。  创建site.js(接口文件,输入:url,输出:html file)  //USAGE: ...

    linkedin-crawler:使用 CasperJS 的简单个人资料图片爬虫

    使用 CasperJS 的简单个人资料图片爬虫 要运行此爬虫,需要 LinedIn 的登录凭据。 运行文件时将它们作为前两个参数传入 casperjs linkedin.js loginEmailAddress loginPassword 还需要标志--ignore-ssl-errors=true...

    webtoon-crawler:Naver 网络漫画爬虫

    网络漫画爬虫 Naver 网络漫画爬虫 卡通内容(图片)爬行 casperjs crawl_contents.js --id=웹툰아이디 --no=회수번호 --dst=대상경로 信息输出到 __stdout__ { " title " : " 해당 화의 제목 " , " rating " : ...

    taobao_cookieman:定时登陆淘宝获取有效cookie

    爬虫脚本模板,通过配置后生成 ${店铺名}_login.js 爬虫文件 依赖: python 2.6+ casperJs 1.0+ 注意:casperJs 依赖 PhantomJS 1.8.2 以上版本 配置: 服务入口: login_robot.py 缺省端口: 9080 使用: 具体API请...

    leetcodepushfront-My-Leetcode-helper:抓取问题和我对本地github存储库的解决方案

    leetcode 推前力码助手 一个小的 nodejs 脚本,可以帮助我从 leetcode ...使用CasperJs作为爬虫,模拟浏览器和用户在Leetcode中登录账号的行为,爬取我的代码。 Cheerio 用于从 CasperJS 返回的 HT

    编程狂人第十三期

    使用python/casperjs编写终极爬虫-客户端App的抓取 2013年度Python运维工具 又被 Python 的 Unicode 坑了 FutureTask 源码解析 C++:在堆上创建对象,还是在栈上? 技术纵横 UPYUN:用Erlang开发的对象存储系统 iOS...

Global site tag (gtag.js) - Google Analytics