zoukankan      html  css  js  c++  java
  • PhantomJs 与 Casperjs

    利用PhantomJS做网页截图经济适用,但其API较少,做其他功能就比较吃力了。

    CasperJs是对phantomjs的一次封装。即phantomjs是原生的,而casperjs是封装在以phantomjs基础上的玩意。

    用CasperJS浏览页面比用PhantomJS更加方便和直观。

    相关传送门:

    # 官网
    http://casperjs.org/

    # github
    https://github.com/casperjs/casperjs

    # 快速入门
    http://docs.casperjs.org/en/latest/quickstart.html

    # API文档
    http://docs.casperjs.org/en/latest/modules/index.html

    # Event事件API文档
    http://docs.casperjs.org/en/latest/events-filters.html#events-reference
    # 利用phantomjs+casperjs实现登陆抓取网页并截图
    http://blog.csdn.net/longhaoyou/article/details/43524977

    # 入门练习demo
    http://blog.csdn.net/kiwi_coder/article/details/36248353

    # 利用nodejs+phantomjs+casperjs采集淘宝商品的价格
    http://www.cnblogs.com/xinzhyu/p/4214669.html

    # 萌萌CasperJS第1篇 1分钟写完爬虫 拿亚马逊商品数据
    http://blog.csdn.net/sagomilk/article/details/20800543

    # 解决乱码问题:

    phantom.outputEncoding = "gbk";

    # 利用open / AJAX发送HTTP请求

    http://docs.casperjs.org/en/latest/modules/casper.html#open

    http://docs.casperjs.org/en/latest/modules/clientutils.html#sendajax

    # 监听页面的错误

    phantom.outputEncoding = "gbk";
    var casper = require('casper').create({
        viewportSize: { 414, height: 736},
        waitTimeout:50000
    });
    
    // 需要先申明,然后再start
    casper.on('page.error', function (err) {
            this.echo(err)
    })
    
    casper.start('http://localhost:8081/#/home', function () {
        this.echo('Casper Starting');
    })
    
    casper.run();

    # 快速判断元素是否存在

    var casper = require('casper').create();
    
    casper.start('http://domain.tld/page.html', function() {
        if (this.exists('h1.page-title')) {
            this.echo('the heading exists');
        }
    });
    
    casper.run();

    # 建议对click之后的验证加入wait操作。这样可以有更好的保证. 并且建议尽可能使用thenClick。除非点击的元素要在指定的区域

      // houseBusinessDetails
        this.thenClick('.listviewItem:nth-child(1) .busitem').wait(1500, function () {
            this.echo(this.getCurrentUrl());
            this.capture('capture/houseBusinessDetails.png')
        })

    # 可以用thenOpen进行跳转界面

      // carBusiness
        this.thenOpen('http://localhost:8081/#/carBusiness').waitForSelector('.listviewItem').wait(300, function () {
            this.echo(this.getCurrentUrl());
            this.capture('capture/carBusiness.png')
        })

    个人练手笔记

    phantom.outputEncoding = "gbk";
    var casper = require('casper').create({
        viewportSize: { 414, height: 736},
        waitTimeout:30000,
    });
    
    const u = function () {
        casper.echo("当前正在操作的URL为:" + casper.getCurrentUrl())
    }
    
    // 需要先申明,然后再start
    casper.on('page.error', function (err) {
         this.echo("出现了error等级的错误信息------- " +  err, 1);
    })
    
    casper.start('http://localhost:8081/', function () {
        // Home
        this.waitForSelector("#app .centers-sec", function () {
            u()
            this.capture('capture/home.png')
        })
    
        // houseBusiness
        this.thenClick('.module:nth-child(1)').wait(1500, function () {
            u()
            this.capture('capture/houseBusiness.png')
        })
    
        // houseBusinessDetails
        this.thenClick('.listviewItem:nth-child(1) .busitem').wait(1500, function () {
            u()
            this.capture('capture/houseBusinessDetails.png')
        })
    
        // carBusiness
        this.thenOpen('http://localhost:8081/#/carBusiness').waitForSelector('.listviewItem').wait(1500, function () {
            u()
            this.capture('capture/carBusiness.png')
        })
    
        // CarBusinessDetails
        this.thenClick('.listviewItem:nth-child(1) .busitem').wait(1500, function () {
            u()
            this.capture('capture/CarBusinessDetails.png')
        })
    
        // lentOutDetail
        this.then(function () {
            if (this.exists('#header .right-btn a')) {
                this.thenClick('#header .right-btn a').wait(1500, function () {
                    u()
                    this.capture('capture/lentOutDetail.png')
                })
            }
        })
    
        // myBusiness
        this.thenOpen('http://localhost:8081/#/myBusiness').wait(1500, function () {
            u()
            this.capture('capture/myBusiness0.png')
        }).thenClick(".mint-tab-item:nth-child(2)").wait(2500, function () {
            u()
            this.capture('capture/myBusiness1.png')
        }).thenClick(".mint-tab-item:nth-child(3)").wait(1000, function () {
            u()
            this.capture('capture/myBusiness2.png')
        })
    
        // userInfo
        this.thenOpen('http://localhost:8081/#/user/userInfo').wait(1500, function () {
            u()
            this.capture('capture/userInfo.png')
        })
    
        // qrcode
        this.thenOpen('http://localhost:8081/#/qrcode').wait(1500, function () {
            u()
            this.capture('capture/qrcode.png')
        })
    
        // allLedger
        this.thenOpen('http://localhost:8081/#/allLedger').wait(3500, function () {
            u()
            this.capture('capture/allLedger.png')
        })
    
        // AutoRepay
        this.thenOpen('http://localhost:8081/#/AutoRepay').wait(3500, function () {
            u()
            this.capture('capture/AutoRepay.png')
        })
    
        // auditAssign
        this.thenOpen('http://localhost:8081/#/auditAssign').wait(2500, function () {
            u()
            this.capture('capture/auditAssign.png')
        })
    })
    
    casper.run();
    View Code
  • 相关阅读:
    wenbao与manacher
    wenbao与拓扑排序
    wenbao与邻接表
    wenbao与最短路floyd
    wenbao与最短路dij
    wenbao与后缀自动机(SAM)
    wenbao与欧拉回路
    wenbao与字符串Hash
    wenbao与后缀数组
    wenbao与扩展kmp
  • 原文地址:https://www.cnblogs.com/CyLee/p/7309381.html
Copyright © 2011-2022 走看看