@aabenoja/phantomjs 中文文档教程
phantomjs
这是 PhantomJS 的 NPM 包装器的一个分支,带有 JS API 的无头 webkit 从 cdn 拉下 v2.0.0 二进制文件。
Building and Installing
npm install phantomjs
或者抓取源代码,
node ./install.js
这个安装程序真正做的只是抓取一个特定的“有福”(通过 这个模块)版本的 Phantom。 随着 Phantom 新版本的发布 并经过审查,该模块将相应更新。
该软件包已设置为获取并运行适用于 MacOS (darwin) 的 Phantom, 基于 Linux 的平台(由 nodejs 标识),以及——从版本 0.2.0 开始—— Windows(感谢 Domenic Denicola)。 如果你 发现任何平台异常,让我们知道或发送补丁。
Running
bin/phantomjs [phantom arguments]
npm 将在 node_modules/.bin
中安装一个指向二进制文件的链接,如下所示 这是惯常做的。
Running via node
该包导出一个 path
字符串,其中包含到 phantomjs 二进制文件/可执行文件。
下面是一个通过节点使用这个包的例子。
var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path
var childArgs = [
path.join(__dirname, 'phantomjs-script.js'),
'some other argument (passed to phantomjs script)'
]
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
// handle results
})
Versioning
主要和次要编号跟踪将要运行的 PhantomJS 的版本 安装。 当有安装程序时,补丁号会增加 幻影二进制文件的更新或补丁版本。
Deciding Where To Get PhantomJS
默认情况下,此包将从 https://bitbucket.org/ariya/phantomjs/downloads
下载 phantomjs。 这对大多数人来说应该很好用。
Downloading from a custom URL
如果 bitbucket 挂了,或者防火墙挡住了 bitbucket,你可能需要使用 下载镜像。 要设置镜像,请设置 npm 配置属性 phantomjs_cdnurl
。 默认为``。
npm install phantomjs --phantomjs_cdnurl=http://cnpmjs.org/downloads
或者将属性添加到您的 .npmrc
文件 (https://www.npmjs.org/doc/files/npmrc.html)
phantomjs_cdnurl=http://cnpmjs.org/downloads
另一种选择是使用 PATH 变量 PHANTOMJS_CDNURL
。
PHANTOMJS_CDNURL=http://cnpmjs.org/downloads npm install phantomjs
Using PhantomJS from disk
如果你打算在一台机器上多次安装phantomjs,你可以 在 PATH 上安装 phantomjs
二进制文件。 安装程序会自动检测 并将其用于非全局安装。
A Note on PhantomJS
PhantomJS 不是 NodeJS 的库。 这是一个单独的环境和代码 为节点编写的不太可能兼容。 特别是 PhantomJS 不公开 Common JS 包加载器。
这是一个 NPM 包装器,可用于方便地使 Phantom 可用 它不是 Node JS 包装器。
我有编写独立 Phantom 脚本的合理经验,我 然后通过在子进程中生成幻影从节点程序中驱动。
阅读 PhantomJS 常见问题解答以获取更多详细信息:http://phantomjs.org/faq.html
Linux Note
关于 Linux 使用的额外说明,来自 PhantomJS 下载页面:
此软件包基于 CentOS 5.8 构建。 它应该在 Lucid 或 更现代的系统(包括其他发行版)。 没有要求 安装 Qt、WebKit 或任何其他库。 不过预计 渲染所需的一些基础库(FreeType、Fontconfig)和 系统中有基本的字体文件。
Troubleshooting
Installation fails with spawn ENOENT
这是 NPM 告诉您它无法启动进程的方式。 它通常意味着:
node
is not on your PATH, or otherwise not correctly installed.tar
is not on your PATH. This package expectstar
on your PATH on Linux-based platforms.
检查您的特定错误消息以获取更多信息。
Installation fails with Error: EPERM
or operation not permitted
or permission denied
这个错误意味着 NPM 无法将 phantomjs 安装到文件系统。 有三种 发生这种情况的主要原因:
- You don't have write access to the installation directory.
- The permissions in the NPM cache got messed up, and you need to run
npm cache clean
to fix them. - You have over-zealous anti-virus software installed, and it's blocking file system writes.
Installation fails with Error: read ECONNRESET
or Error: connect ETIMEDOUT
此错误表示您的互联网连接和安装程序出现问题 无法为您的平台下载 PhantomJS 二进制文件。 请再试一次。
I tried again, but I get ECONNRESET
or ETIMEDOUT
consistently.
你住在中国,还是一个有专制政府的国家? 我们已经看到问题在哪里 GFW 或本地 ISP 阻止 bitbucket,阻止安装程序下载二进制文件。
尝试手动访问下载页面。 如果该页面被阻止,您可以尝试使用不同的 CDN 和 PHANTOMJS_CDNURL
上面描述的环境变量。
I am behind a corporate proxy that uses self-signed SSL certificates to intercept encrypted traffic.
您可以告诉 NPM 和 PhantomJS 安装程序跳过使用 NPM 的 ssl 密钥验证 strict-ssl 设置:
npm set strict-ssl false
警告:关闭 strict-ssl< /code> 让你容易受到攻击者阅读 您的加密流量,因此请自行承担风险!
I tried everything, but my network is b0rked. What do I do?
如果您手动安装 PhantomJS,并将其放在 PATH 上,安装程序将尝试 使用手动安装的二进制文件。
I'm on Debian or Ubuntu, and the installer failed because it couldn't find node
由于一个包,一些 Linux 发行版试图将 node
重命名为 nodejs
冲突。 这是一个不可移植的更改,我们不尝试支持它。 这 官方文档 建议您运行 apt-get install nodejs-legacy
将 node
符号链接到 nodejs
在这些平台上,否则许多 NodeJS 程序将无法正常工作。
Contributing
欢迎提出问题、评论、错误报告和请求请求。 提交他们在 GitHub 上的项目。 如果你还没有贡献 Obvious 项目之前请前往 开源项目并填写 出 OCLA(应该很轻松)。
包含重现步骤(包括代码)的错误报告是 最好的。 更好的是,以拉取请求的形式制作它们。
Author
Dan Pupius (个人网站),支持 The Obvious Corporation。
License
版权所有 2012 The Obvious Corporation。
根据 Apache 许可证 2.0 版获得许可。 查看顶级文件 LICENSE.txt
和 (http://www.apache.org/licenses/LICENSE-2.0)。
phantomjs
This is a fork of the NPM wrapper for PhantomJS, headless webkit with JS API to pull down the v2.0.0 binaries from the cdn.
Building and Installing
npm install phantomjs
Or grab the source and
node ./install.js
What this installer is really doing is just grabbing a particular "blessed" (by this module) version of Phantom. As new versions of Phantom are released and vetted, this module will be updated accordingly.
The package has been set up to fetch and run Phantom for MacOS (darwin), Linux based platforms (as identified by nodejs), and -- as of version 0.2.0 -- Windows (thanks to Domenic Denicola). If you spot any platform weirdnesses, let us know or send a patch.
Running
bin/phantomjs [phantom arguments]
And npm will install a link to the binary in node_modules/.bin
as it is wont to do.
Running via node
The package exports a path
string that contains the path to the phantomjs binary/executable.
Below is an example of using this package via node.
var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path
var childArgs = [
path.join(__dirname, 'phantomjs-script.js'),
'some other argument (passed to phantomjs script)'
]
childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
// handle results
})
Versioning
The major and minor number tracks the version of PhantomJS that will be installed. The patch number is incremented when there is either an installer update or a patch build of the phantom binary.
Deciding Where To Get PhantomJS
By default, this package will download phantomjs from https://bitbucket.org/ariya/phantomjs/downloads
. This should work fine for most people.
Downloading from a custom URL
If bitbucket is down, or the Great Firewall is blocking bitbucket, you may need to use a download mirror. To set a mirror, set npm config property phantomjs_cdnurl
. Default is ``.
npm install phantomjs --phantomjs_cdnurl=http://cnpmjs.org/downloads
Or add property into your .npmrc
file (https://www.npmjs.org/doc/files/npmrc.html)
phantomjs_cdnurl=http://cnpmjs.org/downloads
Another option is to use PATH variable PHANTOMJS_CDNURL
.
PHANTOMJS_CDNURL=http://cnpmjs.org/downloads npm install phantomjs
Using PhantomJS from disk
If you plan to install phantomjs many times on a single machine, you can install the phantomjs
binary on PATH. The installer will automatically detect and use that for non-global installs.
A Note on PhantomJS
PhantomJS is not a library for NodeJS. It's a separate environment and code written for node is unlikely to be compatible. In particular PhantomJS does not expose a Common JS package loader.
This is an NPM wrapper and can be used to conveniently make Phantom available It is not a Node JS wrapper.
I have had reasonable experiences writing standalone Phantom scripts which I then drive from within a node program by spawning phantom in a child process.
Read the PhantomJS FAQ for more details: http://phantomjs.org/faq.html
Linux Note
An extra note on Linux usage, from the PhantomJS download page:
This package is built on CentOS 5.8. It should run successfully on Lucid or more modern systems (including other distributions). There is no requirement to install Qt, WebKit, or any other libraries. It is however expected that some base libraries necessary for rendering (FreeType, Fontconfig) and the basic font files are available in the system.
Troubleshooting
Installation fails with spawn ENOENT
This is NPM's way of telling you that it was not able to start a process. It usually means:
node
is not on your PATH, or otherwise not correctly installed.tar
is not on your PATH. This package expectstar
on your PATH on Linux-based platforms.
Check your specific error message for more information.
Installation fails with Error: EPERM
or operation not permitted
or permission denied
This error means that NPM was not able to install phantomjs to the file system. There are three major reasons why this could happen:
- You don't have write access to the installation directory.
- The permissions in the NPM cache got messed up, and you need to run
npm cache clean
to fix them. - You have over-zealous anti-virus software installed, and it's blocking file system writes.
Installation fails with Error: read ECONNRESET
or Error: connect ETIMEDOUT
This error means that something went wrong with your internet connection, and the installer was not able to download the PhantomJS binary for your platform. Please try again.
I tried again, but I get ECONNRESET
or ETIMEDOUT
consistently.
Do you live in China, or a country with an authoritarian government? We've seen problems where the GFW or local ISP blocks bitbucket, preventing the installer from downloading the binary.
Try visiting the the download page manually. If that page is blocked, you can try using a different CDN with the PHANTOMJS_CDNURL
env variable described above.
I am behind a corporate proxy that uses self-signed SSL certificates to intercept encrypted traffic.
You can tell NPM and the PhantomJS installer to skip validation of ssl keys with NPM's strict-ssl setting:
npm set strict-ssl false
WARNING: Turning off strict-ssl
leaves you vulnerable to attackers reading your encrypted traffic, so run this at your own risk!
I tried everything, but my network is b0rked. What do I do?
If you install PhantomJS manually, and put it on PATH, the installer will try to use the manually-installed binaries.
I'm on Debian or Ubuntu, and the installer failed because it couldn't find node
Some Linux distros tried to rename node
to nodejs
due to a package conflict. This is a non-portable change, and we do not try to support this. The official documentation recommends that you run apt-get install nodejs-legacy
to symlink node
to nodejs
on those platforms, or many NodeJS programs won't work properly.
Contributing
Questions, comments, bug reports, and pull requests are all welcome. Submit them at the project on GitHub. If you haven't contributed to an Obvious project before please head over to the Open Source Project and fill out an OCLA (it should be pretty painless).
Bug reports that include steps-to-reproduce (including code) are the best. Even better, make them in the form of pull requests.
Author
Dan Pupius (personal website), supported by The Obvious Corporation.
License
Copyright 2012 The Obvious Corporation.
Licensed under the Apache License, Version 2.0. See the top-level file LICENSE.txt
and (http://www.apache.org/licenses/LICENSE-2.0).