Home > php教程 > php手册 > body text

PhantomJS简单实例

WBOY
Release: 2016-06-13 11:30:27
Original
1541 people have browsed it

PhantomJS是以WebKit为核心并提供JavaScript编程接口(API)的无界面浏览器。 它提供对web标准的 快速 并且 原生 的支持: DOM操作、CSS选择符、JSON、Canvas和SVG。

1.下载解压PhantomJS  http://phantomjs.org/

2.编写简单的测试代码, 保存为test.js, 解压后phantomjs\examples下有大量实例可供参考

console.log('Loading a web page'<span );
</span><span var</span> page = require('webpage'<span ).create();
</span><span var</span> url = 'http://www.phantomjs.org/'<span ;
page.open(url, </span><span function</span><span  (status) {
    </span><span //</span><span Page is loaded!</span>
    <span if</span> (status !== 'success'<span ) {
        console.log(</span>'Unable to post!'<span );
    } </span><span else</span><span  {
        console.log(page.content);
    }
    phantom.exit();
});</span>
Copy after login

3.因为我使用的是windows, 所以进入CMD命令行模式运行test.js, 并使用管道符将结果输出到test.txt. 如果不用管道的话,会直接输出结果。

.

4.因为PhantomJS支持命令行,所以在开发过程中我们可以使用PHP, Java, 或者Python直接获取结果就OK了

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template