小文笔记 - phantomjs

视频推荐: http://www.intalesson.com/compedium/phantom

2017-05-13

第一节:安装

  Windows安装:
下载解压文件
我的电脑 -> 属性 -> 高级系统设置
点击环境变量
在系统变量窗口中,找到Path变量,选中然后选择编辑
新建后,添加新的环境变量(phantomjs.exe)所在路径
在cmd中,运行phantomjs
Mac安装:
下载解压文件
在用户目录下查看ls -a查看全部文件
打开.bash_profile文件
加入export PATH=$PATH:/Users/suoyong/phantomjs-2.1.1-macosx/bin
在终端中输入phantomjs运行

phantomjs -v 查看版本。

phantomjs a.js 运行 a.js 文件。

phantomjs 进入程序, console.log(1) ,运行 js 代码。

phantom.exit() 退出.

第二节:核心模块

  Web page 模块
文件模块
系统模块
子进程模块
网络服务模块
网站 phantomjs.org/api/

使用 require('模块名') 引用模块。

a.js

  var sys = require('system');

  console.log(sys.args);
phantom.exit();

运行,a.js 是第一个参数,hi 是第二个参数。

  phantomjs a.js hi
a.js,hi

第三节:WebPage对象(一)

  创建WebPage对象:create()
打开网址:page.open
在evaluate方法中操作页面(DOM,JSON,Canvas等)
把当前页面保存成图片:page.render() .create() 创建 webPage 对象实例。
.open() 打开网址。
.evaluate() 在这个里面才能操作 web 界面中的内容。

打开百度首页标题

  var webpage = require('webpage');
var page = webpage.create(); page.open('https://www.baidu.com',function(){ // 打开后做的事
var title = page.evaluate(function(){ // 操作页面
return document.title
});
console.log(title);
phantom.exit(1); // 1 成功 0失败
})

打开对象

  var webpage = require('webpage');
var page = webpage.create(); page.open('https://www.baidu.com',function(){ // 打开后做的事
var title = page.evaluate(function(){ // 操作页面
return document.getElementById('lg');
});
// console.log(title) // [object Object] ,他其他是一个对象
// console.dir(title) // [object Object] ,对象
console.log(JSON.stringify(title)); // 所以需要转换一下
phantom.exit(1); // 1 成功 0失败
})

第三节:WebPage对象(二)

不能在 evaluate 中直接使用 console.log 显示 dom 信息,因为为了不影响其他页面正常运行, evaluate 是运行在沙盒中的,他没有 console.log 。

  var webpage = require('webpage');
var page = webpage.create(); page.open('https://www.baidu.com',function(){
page.evaluate(function(){
console.log(document.getElementById('lg')); // 不会有 console.log() 输出。
});
phantom.exit(1);
})

解决方法:为 webpage 对象添加回调函数。

  var webpage = require('webpage');
var page = webpage.create();
page.onConsoleMessage = function(msg){
console.log(msg);
}
page.open('http://www.intalesson.com/',function(){
page.evaluate(function(){
console.log(document.title);
});
phantom.exit(1);
})

传送参数

如下例添加一个 arg 参数,值为 ‘hi’ 。

  var webpage = require('webpage');
var page = webpage.create();
page.onConsoleMessage = function(msg){
console.log(msg);
}
page.open('http://www.intalesson.com/',function(){
var title = page.evaluate(function(arg){
console.log(arg);
},'hi');
phantom.exit(1);
})

设置 user agent

page.settings.userAgent = '要设置的用户代理'

第五节:提交表单

  • page.onLoadFinished = function(){} 当页面加载完后执行的函数。
  • Dom.submit() 提交。
  • 模仿点击事件 提交。

演示登录智联招聘并截图保存。

  var webpage = require('webpage');
var page = webpage.create(); page.open('http://www.zhaopin.com/',function(){
page.evaluate(function(){
var user = document.getElementById('loginname');
var pass = document.getElementById('password');
user.value = '用户名';
pass.value = '密码';
var submit = document.querySelector('.logbtn button');
var evt = document.createEvent('MouseEvents'); // 创建一个鼠标事件
evt.initMouseEvent('click'); // 初始化一个鼠标点击事件
submit.dispatchEvent(evt); // 使用事件,提交表单
}) page.onLoadFinished = function(status){
if(status == 'success'){ // 检查页面是否加载完毕
page.render('1.png'); // 把页面保存图片
phantom.exit(1);
}
}
})

第六节:操作Cookie

  • cookies 查看
  • addCookie 设置
    当设置 cookie 时一定要设置 domain name value 三个值。
  phantom.addCookie({'domain':'.baidu.com','name':'xw','value':'1'});
console.log(JSON.stringify(phantom.cookies));
phantom.exit(1);

第七节:CasperJS

CasperJS 扩展自 phantomJS ,可更简单的操作页面元素。

phantomjs 不是 nodejs 的模块,但 casperjs 可使用 npm 安装。

  • casper.start() = page.open()
  • echo() = console.log()

phantomjs 像 js , casperjs 像 jq 。

  • 安装 casperjs npm install -g casperjs

第八节:步进式脚本语言

小文注

乱码问题: http://blog.csdn.net/kaosini/article/details/47252457

  • 方法一:
    在 js 文件中添加 phantom.outputEncoding="gbk" 可解决乱码。
  • 方法二:
    phantomjs --output-encoding=gbk a.js

小文笔记 - phantomjs的更多相关文章

  1. casperjs-options

    The Casper class The easiest way to get a casper instance is to use the module's create() method: 最简 ...

  2. capserjs-prototype(下)

    scrollTo() 具体样式: scrollTo(Number x, Number y) New in version 1.1-beta3. Scrolls current document to ...

  3. 笔记-python-selenium,phantomjs

    笔记-python-selenium,phantomjs 1.      简介 1.1.    selenium selenium是一款自动化测试工具,支持多种语言 为什么爬虫要使用selenium呢 ...

  4. PhantomJS笔记,Node.js集成PhantomJS

    PhantomJS笔记,Node.js集成PhantomJS 转 https://www.linchaoqun.com/html/cms/content.jsp?menu=index&id=1 ...

  5. 今天折腾phantomjs+selenium的笔记

    1.debian8里安装phantomjs的方法: 参照:http://www.cnblogs.com/lgh344902118/p/6369054.html a.去https://bitbucket ...

  6. Web前端学习笔记之安装和使用PhantomJS

    0x00 安装PhantomJS(linux环境安装) 将PhantomJS下载在/usr/local/src/packet/目录下(这个看个人喜好) 操作系统:CentOS 7 64-bit 1.下 ...

  7. selenium&PhantomJS笔记

    配置pip文件 Windows下pip 配置文件的位置%HOME%/pip/pip.ini linux下安装pip,以Debian Linux为例su -apt-get install python- ...

  8. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  9. 笔记之Python网络数据采集

    笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...

随机推荐

  1. GO slim

    1. GO slim简介 GO slims are cut-down versions of the GO ontologies containing a subset of the terms in ...

  2. python 什么是位置参数?

    位置参数是必选参数 ----不能不传, ----不能传一部分, ---必须按顺序传 ----必须传全部参数

  3. 创建一个简单的WCF程序

    1.创建WCF服务库 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→WCF→WCF服务库,然后输入项目名称(Name),存放位置(Location ...

  4. cookie 和 session 的一些事 中间件

    cookie 和 session cookie 1. 保存在浏览器上一组组键值对,服务器让浏览器进行设置. 2. 为什么要用cookie? HTTP协议是无状态.使用cookie保存状态. 3. dj ...

  5. IDEA添加作者和时间信息

  6. 从源码层面聊聊面试问烂了的 Spring AOP与SpringMVC

    Spring AOP ,SpringMVC ,这两个应该是国内面试必问题,网上有很多答案,其实背背就可以.但今天笔者带大家一起深入浅出源码,看看他的原理.以期让印象更加深刻,面试的时候游刃有余. Sp ...

  7. e3.7.2-MyEclipse-10.7安装SVN插件

    MyEclipse 10.7的版本是:e3.7.2,要求是匹配该插件eclipse_svn_site-1.10.1的版本,否则无效 将eclipse_svn_site-1.10.1插件文件夹直接拷贝到 ...

  8. window.open()居中显示

    function openwindow(url,name,iWidth,iHeight){ // url 转向网页的地址 // name 网页名称,可为空 // iWidth 弹出窗口的宽度 // i ...

  9. Elasticstarch 相关

    索引: 在Elasticsearch中存储数据的行为就叫做索引(indexing),不过在索引之前,我们需要明确数据应该存储在哪里. 在Elasticsearch中,文档归属于一种类型(type),而 ...

  10. Linux 命令梳理

    Linux 命令梳理 待梳理命令 nohup 用户管理 useradd 新建用户 sudo useradd {user name} -s /bin/bash -d /data/{user name} ...