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

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

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

相关传送门:

  1. # 官网
    http://casperjs.org/
  2.  
  3. # github
  4. https://github.com/casperjs/casperjs
  5.  
  6. # 快速入门
    http://docs.casperjs.org/en/latest/quickstart.html
  7.  
  8. # API文档
    http://docs.casperjs.org/en/latest/modules/index.html
  9.  
  10. # Event事件API文档
    http://docs.casperjs.org/en/latest/events-filters.html#events-reference
  1. # 利用phantomjs+casperjs实现登陆抓取网页并截图
  2. http://blog.csdn.net/longhaoyou/article/details/43524977
  3.  
  4. # 入门练习demo
    http://blog.csdn.net/kiwi_coder/article/details/36248353
  5.  
  6. # 利用nodejs+phantomjs+casperjs采集淘宝商品的价格
    http://www.cnblogs.com/xinzhyu/p/4214669.html
  7.  
  8. # 萌萌CasperJS第1篇 1分钟写完爬虫 拿亚马逊商品数据
    http://blog.csdn.net/sagomilk/article/details/20800543

# 解决乱码问题:

  1. phantom.outputEncoding = "gbk";

# 利用open / AJAX发送HTTP请求

  1. http://docs.casperjs.org/en/latest/modules/casper.html#open
  2.  
  3. http://docs.casperjs.org/en/latest/modules/clientutils.html#sendajax

# 监听页面的错误

  1. phantom.outputEncoding = "gbk";
  2. var casper = require('casper').create({
  3. viewportSize: {width: , height: },
  4. waitTimeout:
  5. });
  6.  
  7. // 需要先申明,然后再start
  8. casper.on('page.error', function (err) {
  9. this.echo(err)
  10. })
  11.  
  12. casper.start('http://localhost:8081/#/home', function () {
  13. this.echo('Casper Starting');
  14. })
  15.  
  16. casper.run();

# 快速判断元素是否存在

  1. var casper = require('casper').create();
  2.  
  3. casper.start('http://domain.tld/page.html', function() {
  4. if (this.exists('h1.page-title')) {
  5. this.echo('the heading exists');
  6. }
  7. });
  8.  
  9. casper.run();

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

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

# 可以用thenOpen进行跳转界面

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

个人练手笔记

  1. phantom.outputEncoding = "gbk";
  2. var casper = require('casper').create({
  3. viewportSize: {width: 414, height: 736},
  4. waitTimeout:30000,
  5. });
  6.  
  7. const u = function () {
  8. casper.echo("当前正在操作的URL为:" + casper.getCurrentUrl())
  9. }
  10.  
  11. // 需要先申明,然后再start
  12. casper.on('page.error', function (err) {
  13. this.echo("出现了error等级的错误信息------- " + err, 1);
  14. })
  15.  
  16. casper.start('http://localhost:8081/', function () {
  17. // Home
  18. this.waitForSelector("#app .centers-sec", function () {
  19. u()
  20. this.capture('capture/home.png')
  21. })
  22.  
  23. // houseBusiness
  24. this.thenClick('.module:nth-child(1)').wait(1500, function () {
  25. u()
  26. this.capture('capture/houseBusiness.png')
  27. })
  28.  
  29. // houseBusinessDetails
  30. this.thenClick('.listviewItem:nth-child(1) .busitem').wait(1500, function () {
  31. u()
  32. this.capture('capture/houseBusinessDetails.png')
  33. })
  34.  
  35. // carBusiness
  36. this.thenOpen('http://localhost:8081/#/carBusiness').waitForSelector('.listviewItem').wait(1500, function () {
  37. u()
  38. this.capture('capture/carBusiness.png')
  39. })
  40.  
  41. // CarBusinessDetails
  42. this.thenClick('.listviewItem:nth-child(1) .busitem').wait(1500, function () {
  43. u()
  44. this.capture('capture/CarBusinessDetails.png')
  45. })
  46.  
  47. // lentOutDetail
  48. this.then(function () {
  49. if (this.exists('#header .right-btn a')) {
  50. this.thenClick('#header .right-btn a').wait(1500, function () {
  51. u()
  52. this.capture('capture/lentOutDetail.png')
  53. })
  54. }
  55. })
  56.  
  57. // myBusiness
  58. this.thenOpen('http://localhost:8081/#/myBusiness').wait(1500, function () {
  59. u()
  60. this.capture('capture/myBusiness0.png')
  61. }).thenClick(".mint-tab-item:nth-child(2)").wait(2500, function () {
  62. u()
  63. this.capture('capture/myBusiness1.png')
  64. }).thenClick(".mint-tab-item:nth-child(3)").wait(1000, function () {
  65. u()
  66. this.capture('capture/myBusiness2.png')
  67. })
  68.  
  69. // userInfo
  70. this.thenOpen('http://localhost:8081/#/user/userInfo').wait(1500, function () {
  71. u()
  72. this.capture('capture/userInfo.png')
  73. })
  74.  
  75. // qrcode
  76. this.thenOpen('http://localhost:8081/#/qrcode').wait(1500, function () {
  77. u()
  78. this.capture('capture/qrcode.png')
  79. })
  80.  
  81. // allLedger
  82. this.thenOpen('http://localhost:8081/#/allLedger').wait(3500, function () {
  83. u()
  84. this.capture('capture/allLedger.png')
  85. })
  86.  
  87. // AutoRepay
  88. this.thenOpen('http://localhost:8081/#/AutoRepay').wait(3500, function () {
  89. u()
  90. this.capture('capture/AutoRepay.png')
  91. })
  92.  
  93. // auditAssign
  94. this.thenOpen('http://localhost:8081/#/auditAssign').wait(2500, function () {
  95. u()
  96. this.capture('capture/auditAssign.png')
  97. })
  98. })
  99.  
  100. casper.run();

PhantomJs 与 Casperjs的更多相关文章

  1. [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)

    最近在使用Python爬取网页内容时,总是遇到JS临时加载.动态获取网页信息的困难.例如爬取CSDN下载资源评论.搜狐图片中的“原图”等,此时尝试学习Phantomjs和CasperJS来解决这个问题 ...

  2. PhantomJS、CasperJS安装配置图文详解

    目前网站主流的加载方式: 一种是同步加载:另一种是异步加载,也即我们常说的用ajax.对于同步加载的网站,普通的爬虫程序轻松就能搞定.但是对于那种异步请求数据的网站,通常使用selenium+Phan ...

  3. PhantomJS与CasperJS在Windows下的安装与使用

    按照网上的教程来呢,一定是不好使的,这是常理. 所以必须要告诉你怎么使用Phantomjs…… 这么用! 1.下载Phantomjs的压缩包并解压缩: 2.在bin目录(包含phantomjs.exe ...

  4. 前端端对端测试:基于PhantomJS的CasperJS

    简介 Casperjs是一个基于PhantomJS和SlimerJS的前端端对端测试框架,当然你也可以使用它完成网络爬虫功能,它的特点的通过简单的脚本模拟浏览器行为, 主要有casper.tester ...

  5. 浏览器自动化测试初探 - 使用phantomjs与casperjs

    收录待用,修改转载已取得腾讯云授权 作者:yangchunwen 首先要解释一下为什么叫浏览器自动化测试,因为本文只关注发布后页面功能的自动化测试,也就是UI层面的自动化. 浏览器测试有别于js代码的 ...

  6. [转] 浏览器自动化测试初探:使用 phantomjs 与 casperjs

    [From] https://www.qcloud.com/community/article/641602001489391648 作者:yangchunwen 首先要解释一下为什么叫浏览器自动化测 ...

  7. Phantomjs和Casperjs,后台网页抓取和交互

    var casper = require('casper').create({ verbose: true, logLevel: 'debug', pageSettings: { loadImages ...

  8. windows下面安装casperjs

    因为需要 就学习了一下casperjs,CasperJS是一个开源的导航脚本处理和测试工具,基于PhantomJS(前端自动化测试工具)编写.由于casperjs对PhantomJS的依赖性,所以需要 ...

  9. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

    前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...

随机推荐

  1. 【Luogu P2201】【JZOJ 3922】数列编辑器

    题面: Description 小Z是一个爱好数学的小学生.最近,他在研究一些关于整数数列的性质. 为了方便他的研究,小Z希望实现一个叫做"Open Continuous Lines Pro ...

  2. Tarjan水题系列(4):HAOI2010 软件安装

    题目: 现在我们的手头有N个软件,对于一个软件i,它要占用Wi​的磁盘空间,它的价值为Vi​.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi​的和最大). ...

  3. flask之上下文管理

    简单来说,falsk上下文管理可以分为三个阶段: 1.请求进来时,将请求鞋底的相关数据放入上下文管理中进行管理 2.在视图函数中,要去上下文管理中取值 3.请求响应之后,要将上下文管理中的数据清除 详 ...

  4. java调用webservice接口 几种方法

    webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...

  5. SVN本地搭建服务

    本地搭建svn 服务端并使用 1.下载SVN服务端软  VisualSVN server 地址:http://subversion.apache.org/packages.html 2.安装 Visu ...

  6. Git Flow 的正确使用姿势

    https://www.jianshu.com/p/41910dc6ef29 Git Flow 的概念 在使用Git的过程中如果没有清晰流程和规划,否则,每个人都提交一堆杂乱无章的commit,项目很 ...

  7. python基础练习题3

    01:有1,2,3,4个数字,能组成多少个互不相同且无重复的三位数,都是多少思路:可填写在百位,十位,个位的数字都是1,2,3,4.组成所有的排列后再去掉不满足条件的排列 list =[,,,] li ...

  8. flask-migrate的使用

    先安装flask-migrate:pip install flask-migrate 然后见代码: 输入命令生成migrates文件夹 然后可以看到项目下生成文件夹: 然后输入命令: 看到: 总之,模 ...

  9. layui token 过期 重新登陆

    这个方法你要全局设置     //jquery全局配置 $.ajaxSetup({     cache: false,     crossDomain: true,       headers :{' ...

  10. FAT12 img tool

    NJU/2019/OS Description: CODE: Main.cpp: /* @author: Edwin Xu @Date:2019/11/13 @Note: just ASCII */ ...