crawler.js | |
---|---|
Crawler.js is a web crawler written in JavaScript/PhantomJS. Originally developer for pager.js. Source code on GitHub. MIT License. | (function () { |
Get all arguments passed to crawler | var args = require("system").args; |
Set the start argument index to 1 (since 0 is the name of the file) | var index = 1; |
Find out if any URL pattern should be ignored while crawling the site | var ignorePatterns = [];
if (args[index] === '-i' || args[index] === '--ignore') {
ignorePatterns.push(args[1]);
index += 2;
} |
Fetch the URL from the argument list | var startUrl = args[index]; |
Add the start URL to the list of total URLs
and to the list of not yet visited pages.
| var pages = [startUrl];
var notVisitedPages = [startUrl];
var visitingPages = 0; |
| var notVisitingPages = []; |
If no arguments are provided: print the help information: | if (startUrl === null) {
console.log("Usage: phantomjs --load-images=no crawler.js [options] url");
console.log("");
console.log("Options:");
console.log(" --ignore, -i url pattern to ignore");
console.log("");
console.log("Example:");
console.log(" phantomjs --load-images=no crawler.js -i /some/url/\d* http://example.com/ ");
return;
} |
Include the file system module. We'll need it when storing files to disc later | var fs = require("fs"); |
Include the webpage module. This module can create web pages! | var webpage = require('webpage'); |
Load the scripts jquery ($), q (Q) and underscore (_) so we can use them | phantom.injectJs('jquery-1.8.2.min.js');
phantom.injectJs('q.min.js');
phantom.injectJs('underscore-min.js'); |
Helper method for removing an item from an array Used like | function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax = arr.indexOf(what)) != -1) {
arr.splice(ax, 1);
}
}
return arr;
} |
processPageThis method is called with a loaded page instance and the URL of that page instance. | var processPage = function (page, url) { |
saveHref
| var actions = { |
The | saveHref:function (href) { |
check if href contains #!/ | if (href && href.indexOf('#!/') !== -1) { |
check if URI is inside | if (href.indexOf(startUrl) !== -1) { |
make sure URL isn't in pages-array already | if (!_.contains(pages, href)) { |
check if the URL should be ignored | if (!_.any(ignorePatterns, function (pattern) {
return href.match(pattern) != null;
})) { |
if not: add to pages-array and notVisitedPages | console.error("adding URL: " + href);
pages.push(href);
notVisitedPages.push(href);
if (visitingPages < 2) {
visitingPages++;
processUrl(href);
} else {
notVisitingPages.push(href);
}
}
}
}
}
}
}; |
page.onCallbackReact to | page.onCallback = function (obj) {
if (obj.action) {
actions[obj.action](obj.data);
}
}; |
page.evaluateEvaluate the page and run the callback in the scope of the web page.
Observe that the callback cannot access varialbes in this script that are outside
the callback! Instead the method must use | var htmlCode = page.evaluate(function () {
var htmlTag = $('html', document); |
| var links = $('a', htmlTag);
$.each(links, function (linkIndex, link) {
var href = link.href;
window.callPhantom({
action:'saveHref',
data:href
});
}); |
| var hidden = $('body', htmlTag).find(':hidden');
$.each(hidden, function (hiddenIndex, hiddenElement) {
var $el = $(hiddenElement);
if ($el.css('visibility') === 'hidden' || $el.css('display') === 'none') {
$el.remove();
}
}); |
| $('script', htmlTag).remove();
$('link', htmlTag).remove(); |
Return the HTML content in the cleaned up HTML site as a string | return htmlTag[0].innerHTML
}); |
Save contentSave polished HTML as {absolute-URL}escapedfragment_/hash/bang/value/index.html E.g. http://example.com/#!/some/cool/page is stored as escapedfragment_/some/cool/page/index.html | var compactHtml = _.compact(htmlCode.split('\n')).join('\n');
var totalFileName = page.url.substring(startUrl.length);
var decodedParameter = totalFileName.split('#!/')[1];
var parameter = decodedParameter ? decodedParameter : '';
var folderName = '_escaped_fragment_' + totalFileName.split('#!/')[0];
var totalFolderName = folderName + (parameter ? fs.separator + parameter : ''); |
create folder/tree with name | fs.makeTree(totalFolderName); |
create file index.html in folder with the content | fs.write(totalFolderName + fs.separator + 'index.html', compactHtml, 'w');
removeA(notVisitedPages, url);
visitingPages--; |
Release the web page | page.release(); |
Are there any pages left to visit and are less that 2 pages visited at the moment? | if (notVisitingPages.length > 0) {
if (visitingPages < 2) {
var lastUrl = notVisitingPages.pop();
visitingPages++; |
Then visit a not yet visited page. | processUrl(lastUrl);
}
}
}; |
processUrlProcess the URL provided. Start by loggins the URL to console. Then create a new PhantomJS headless WebKit browser page. | var processUrl = function (url) {
console.log("processing url: " + url);
var page = webpage.create();
var pageIsOpened = $.Deferred(); |
Load the web page. | page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
pageIsOpened.resolve(page);
}
}); |
Wait for 3 seconds once the page is loaded and then
| pageIsOpened.done(function (page) {
setTimeout(function () {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', function () {
processPage(page, url);
});
}, 3000);
});
}; |
StartStart processing the start URL. | processUrl(startUrl); |
ExitAsk every second if all pages are visited. If so: exit phantom | setInterval(function () {
if (notVisitedPages.length === 0) {
phantom.exit();
}
}, 1000);
}());
|