PhantomJS 学习笔记(1):利用googlemap获取路径导航
27 Nov 2012闲来无事,网上东瞅西瞅,偶尔碰到PhantomJS,一股新鲜感油然而生,遂折腾,此文为记。
官网:http://phantomjs.org/
文档:https://github.com/ariya/phantomjs/wiki
Github : https://github.com/ariya/phantomjs
关于PhantomJS:
PhantomJS (www.phantomjs.org) is a headless WebKit scriptable with JavaScript or CoffeeScript. It is used by hundreds of developers and dozens of organizations for web-related development workflow.
The latest stable release is version 1.7 (codenamed “Blazing Star”). Follow the official Twitter stream @PhantomJS to get the frequent development updates.
Note: Please do not create a GitHub pull request without reading the Contribution Guide first. Failure to do so may result in the rejection of the pull request.
意思就是:phantomjs 是 一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,这样访问网页就省去了浏览器的界面绘制所消耗的系统资源,比较适合用于网络测试等应用 。
下边贴出官方的一个demo:
// Get driving direction using Google Directions API. var page = require('webpage').create(), system = require('system'), origin, dest, steps; if (system.args.length < 3) { console.log('Usage: direction.js origin destination'); console.log('Example: direction.js "San Diego" "Palo Alto"'); phantom.exit(1); } else { origin = system.args[1]; dest = system.args[2]; page.open(encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin + '&destination=' + dest + '&units=imperial&mode=driving&sensor=false'), function (status) { if (status !== 'success') { console.log('Unable to access network'); } else { steps = page.content.match(/<html_instructions>(.*)<\/html_instructions>/ig); if (steps == null) { console.log('No data available for ' + origin + ' to ' + dest); } else { steps.forEach(function (ins) { ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>'); ins = ins.replace(/\<div/ig, '\n<div'); ins = ins.replace(/<.*?>/g, ''); console.log(ins); }); console.log(''); console.log(page.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, '')); } } phantom.exit(); }); }
运行截图:
更多好玩的demo自己可以下载源码来折腾~
玩完以后可以自己写个app,搞个什么好捏?