Skip to content

Instantly share code, notes, and snippets.

@phil-monroe
Last active November 13, 2018 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phil-monroe/ceff3b2e5b9cd893cfc05a17d2d7ee0c to your computer and use it in GitHub Desktop.
Save phil-monroe/ceff3b2e5b9cd893cfc05a17d2d7ee0c to your computer and use it in GitHub Desktop.
Script to test apify page functions

Usage:

phantomjs apify-tester.js
var page = require('webpage').create();
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg);
};
page.open('https://seneweb.senegence.com/us/products/anti-aging/collagen-night-pak/', function() {
page.includeJs("https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js", function() {
page.evaluate(function() {
// SCRAPER LOGIC GOES HERE
function pageFunction(context) {
var price = context.jQuery('#p-font-price span.p-font-price').text().trim();
console.log("price: " + price);
return {
'foo': 'bar',
'price': price
};
}
var result = pageFunction({
request: {
label: '', // label of actual url
url: window.location.href
},
willFinishLater: function () { console.log('(willFinishLater)', arguments) },
finish: function () { console.log('(finish)', arguments) },
enqueuePage: function () { console.log('(enqueuePage)', arguments) },
skipOutput: function () { console.log('(skipOutput)', arguments) },
skipLinks: function () { console.log('(skipLinks)', arguments) },
jQuery: $
});
console.log('Result: ' + JSON.stringify(result));
});
phantom.exit();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment