Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
REMitchell
GitHub Repository: REMitchell/python-crawling
Path: blob/master/6-Selenium/phantomjs/examples/direction.js
164 views
1
// Get driving direction using Google Directions API.
2
3
var page = require('webpage').create(),
4
system = require('system'),
5
origin, dest, steps;
6
7
if (system.args.length < 3) {
8
console.log('Usage: direction.js origin destination');
9
console.log('Example: direction.js "San Diego" "Palo Alto"');
10
phantom.exit(1);
11
} else {
12
origin = system.args[1];
13
dest = system.args[2];
14
page.open(encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin +
15
'&destination=' + dest + '&units=imperial&mode=driving&sensor=false'), function (status) {
16
if (status !== 'success') {
17
console.log('Unable to access network');
18
} else {
19
steps = page.content.match(/<html_instructions>(.*)<\/html_instructions>/ig);
20
if (steps == null) {
21
console.log('No data available for ' + origin + ' to ' + dest);
22
} else {
23
steps.forEach(function (ins) {
24
ins = ins.replace(/\&lt;/ig, '<').replace(/\&gt;/ig, '>');
25
ins = ins.replace(/\<div/ig, '\n<div');
26
ins = ins.replace(/<.*?>/g, '');
27
console.log(ins);
28
});
29
console.log('');
30
console.log(page.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, ''));
31
}
32
}
33
phantom.exit();
34
});
35
}
36
37