How to Scrape Websites Using PhantomJS

Demystifying PhantomJS

PhantomJS is a "headless" web browser. This means it has no graphical user interface (GUI); instead, it operates purely via scripts, making it more compact, faster, and thus more efficient. It can automate various tasks using JavaScript (JS), such as code testing or data collection. 

For beginners, we recommend first installing PhantomJS on your computer using the 'npm' command in the CLI. This can be done by running the following command:

npm install phantomjs -g 

Now the 'phantomjs' command will be available for use.

Pros and Cons of Using PhantomJS for Data Collection 

PhantomJS has many advantages, including being "headless," which, as explained above, makes it faster since there's no need to load graphics for testing or retrieving information. 

PhantomJS can be effectively used for:

1. Screen Capture

PhantomJS allows you to automate the process of capturing and saving PNG, JPEG, and even GIF files. This feature greatly simplifies tasks related to ensuring user interface/user experience. For example, you can run the command 'phantomjs amazon.js' to capture images of competitor product listings or to verify that your company's product listings appear correctly. 

2. Page Automation

This is a major advantage of PhantomJS as it helps developers save a lot of time. By running commands like 'phantomjs userAgent.js', developers can write and test JS code against a specific web page. The key time-saving benefit is that this process can be automated and executed without needing to open a browser.

3. Testing

PhantomJS is advantageous for website testing because it simplifies the process, similar to other popular web scraping tools like Selenium. Headless browsing without a GUI means that scanning for issues can happen faster, and error codes are detected and reported at the command line level. 

Developers also integrate PhantomJS with various continuous integration (CI) systems to test code before it goes live. This helps developers fix bugs in real time, ensuring smoother project operation.

4. Network Monitoring / Data Collection

PhantomJS can also be used for monitoring network traffic/activity. Many developers program it to collect targeted data such as:

  • Performance of a specific web page
  • Addition/removal of code lines
  • Stock price fluctuation data 
  • Influence/engagement data when scraping websites like TikTok.

The disadvantages of using PhantomJS include:

  • It can be used by malicious actors to carry out automated attacks (mainly because it does not use a user interface) 
  • Difficulties may arise when performing full-cycle, end-to-end, and functional testing.

Step-by-Step Guide to Data Collection with PhantomJS

PhantomJS is widely popular among NodeJS developers, so we provide an example of its use in a NodeJS environment. The example shows the process of retrieving HTML content from a URL.

Step One: Setting up package.json and Installing npm Packages

Create a project folder and a "package.json" file inside it.

{
"name": "phantomjs-example",
"version": "1.0.0",
"title": "PhantomJS Example",
"description": "PhantomJS Example",
"keywords": [
  [ "пример фантома"
],
"main": "./index.js",
"scripts": {
"inst": "rm -rf node_modules && rm package-lock.json && npm install",
"dev": "nodemon index.js"
},
"dependencies": {
"phantom": "^6.3.0"
}
}

Then run the following command in the terminal: $ npm install. This will install Phantom into the local project folder 'node_modules'.

Step Two: Creating a Phantom JS Script

Create a JS script and name it "index.js".

const phantom = require('phantom');

const main = async () => {
  const instance = await phantom.create();
  const page = await instance.createPage();
  await page.on('onResourceRequested', function(requestData) {
    console.info('Requesting', requestData.url);
  });

  const url = 'https://example.com/'; 
  console.log('URL::', url);

  const status = await page.open(url);
  console.log('STATUS::', status);

  const content = await page.property('content');
  console.log('CONTENT::', content);

  await instance.exit();
};

main().catch(console.log);

Step Three: Running the JS Script

To run the script, execute the following command in the terminal: $ node index.js. As a result, you will get the desired data in the form of HTML content.

Data Automation: Simpler Alternatives to Manual Scraping

When it comes to large-scale scraping of data, some companies may prefer to use alternatives to PhantomJS. 

  • Proxy Servers: Web scraping using proxy servers can be beneficial because it allows data collection at scale by sending an unlimited number of simultaneous requests. Proxy servers can also help overcome blocking issues on target websites, such as rate limiting or geo-blocking. In this case, companies can use mobile and residential IP addresses/devices based on country/city to route data requests, enabling more accurate user data (e.g., competitor prices, ad campaigns, and Google search results).
  • Ready-to-Use Datasets: Datasets are essentially "information packages" that have already been collected and are ready to be passed to algorithms/teams for immediate use. They typically include information from the target website and are enriched with data from relevant sites on the internet (e.g., product information in a given category from multiple sellers across various eCom marketplaces). Datasets can also be periodically updated to ensure all data points remain current. The main advantage is that no time/resources are spent on data collection, meaning more time can be devoted to data analysis and creating value for clients.
  • Fully Automated Web Scraper IDE Environment: Web Scraper IDE is a customizable, zero-code, zero-infrastructure data collection solution. Data scrapers allow companies to take an active role in the data collection process without the headache of developing and maintaining software/hardware. The three-step process includes selecting a target website, choosing when to collect data (real-time or on a schedule), selecting the output format (JSON, CSV, HTML, or Microsoft Excel), and delivering the data to a convenient location (webhook, email, Amazon S3, Google Cloud, Microsoft Azure, SFTP, or API).