Search code examples
javascripthtmlweb-scrapingphantomjsshared-hosting

Running a phantomjs program on shared hosting?


I have installed phantomjs on my windows 8.1 pc, and i have done some scraping by executing the js file from command prompt. The question now is how do i run phantomjs program in shared hosting. I have looked on the web for a solution and all i could find is "I would suggest moving the PhantomJS binary file to your home directory. Once there, you can execute PhantomJS by simply pointing to the file: ~/phantomjs -v.". But i cant understand what "~/ phantomjs -v" means. Where and how do i write and run this code? to be clear here is the javascript file

    var page = new WebPage()
    var fs = require('fs');

    page.onLoadFinished = function() {
    console.log("page load finished");
    page.render('export.png');
    fs.write('1.html', page.content, 'w');
    phantom.exit();
    };
    page.open("https://url", function() {
    page.evaluate(function() {
    });
    });

Now how do i run this file on my shared hosting (linux 64-bit) and what is "~/ phantomjs -v"?


Solution

  • ~ is a shortcut to a user's home directory.

    ~/phantomjs is like saying "binary file with the name phantomjs that is located in my home directory".

    To run a scrape you need to launch PhantomJS and feed it the name of a script. So in your case you would put PhantomJS and a script for it into your home directory and then execute this command from the shell:

    ~/phantomjs ~/script.js
    

    That is "launch phantomjs that is in my home directory and let it run a script that is also in my home directory".

    If you want to launch this command from another, say PHP, script you could use shell_exec command in PHP.

    But be wary of restrictions that shared hosting imposes — usually they limit execution time and CPU consumption, which in case of PhantomJS may be quite high. I'd recommend that you go with a VPS for this kind of work.