PhantomJS - 模拟浏览器加载页面

Phantomjs是什么?


Phantomjs is a webkit with javascript api which means using phantomjs we can load web pages emulating like a webkit based browser without any GUI.

In simple words for understanding, Phantomjs can access and load webpages behaving like a normal browser.It can render webpages exactly same as browser which is the unique and foremost feature of phantomjs.Developers call this concept as headless webkit.

from Phantomjs - Webkit browser built on Javascript

更多详细介绍参考 Phantomjs官网

 

Phantomjs可以干什么?


1.作为一个js的命令行解释器运行js脚本,就行python一样,只不过编程语言改用javascript,对于熟悉javascript的同学,也是一个不错的选择;
2.模拟浏览器行为:
这一功能应用场景就多了,例如从其他服务器拉取到html页面后,通过Phantomjs执行页面中的javascript,生成最终展示给用户的html页面。
新浪微博目前没有提供API给开发者直接搜索微博内容,想做新浪微博搜索的同学可以使用Phantomjs间接获得搜索内容。
其实可以专门搭建一个Web服务,用户提交url地址,服务返回解析过javascript后的最终html页面。这种需求应该还挺多,不知道现在是不是已经有这种服务了?

其他应用场景包括网络爬虫、测试等等。