Creating and deploying web scraper using Apify
Tags: webdev, webscraping, javascript, programming
Published: 2024-03-27 09:07:55




**Introducing web scraper and Apify**
Let's get started by Understanding what [Web Scraping](https://www.imperva.com/learn/application-security/web-scraping-attack/#:~:text=Web%20scraping%20is%20the%20process,replicate%20entire%20website%20content%20elsewhere.) is.

Web Scraping is a technique for extracting data from websites leading to insightful information to developers, businesses, and organizations for diverse purposes. It can extract underlying HTML code, data stored in the database and then be able to replicate the entire website elsewhere.

We can go ahead and know about [Apify](https://www.googleadservices.com/pagead/aclk?sa=L&ai=DChcSEwichpv3goWFAxXRkYMHHZQ3Bl4YABAAGgJlZg&ase=2&gclid=CjwKCAjwte-vBhBFEiwAQSv_xeH4m4dU33B_ukd9gF47vDcLJH3BsaXWaBJd4zp2l30AEJtmvUWIuxoCpvwQAvD_BwE&ohost=www.google.com&cid=CAESV-D2fpgttfCXAEiedgenh8imeVIsbn7dxr8VKC_SjY1g07_BSlSzXIuZNzCzqKER97vjLqPbA0_sbGK2XRCVZQNoqUu3ZbO6DTrH_v3_mkVeFKInIxFuBg&sig=AOD64_2qchqDW2FyHe1x6Eh6vpY8I02I2Q&q&nis=4&adurl&ved=2ahUKEwiv6JP3goWFAxVYQ_EDHVDSCvMQ0Qx6BAgGEAE). Apify is a platform that is helpful to developers as it provides powerful tools and templates for building, deploying, and publishing web scraping, data extraction, and web automation tools efficiently. 

In this guide we will walk through processes of creating and deploying web scrapers using Apify SDK for JavaScript.

**Understanding the template-generated code** 
In this project we will use a JavaScript template which utilizes various libraries such as Axios, Cheerio, and the Apify SDK which we will be discussing. Let's delve into the code before we go ahead to understand it.

```
// Axios - Promise based HTTP client for the browser and node.js
import axios from 'axios';

// Cheerio - The fast, flexible & elegant library for parsing and manipulating HTML and XML
import * as cheerio from 'cheerio';

// Apify SDK - toolkit for building Apify Actors
import { Actor } from 'apify';

// Initialize the Actor environment
await Actor.init();

// Get input data (e.g., URL to scrape)
const input = await Actor.getInput();
const { url } = input;

// Fetch the HTML content of the page.
const response = await axios.get(url);

// Parse the downloaded HTML with Cheerio to enable data extraction.
const $ = cheerio.load(response.data);

// Extract all headings from the page (tag name and text).
const headings = [];
$("h1, h2, h3, h4, h5, h6").each((i, element) => {
    const headingObject = {
        level: $(element).prop("tagName").toLowerCase(),
        text: $(element).text(),
    };
    console.log("Extracted heading", headingObject);
    headings.push(headingObject);
});

// Save headings to Dataset - a table-like storage.
await Actor.pushData(headings);

// Gracefully exit the Actor process.
await Actor.exit();
```



**Used libraries** 
[Axios](https://axios-http.com/docs/intro) - it is a promise HTTP clients to make requests to the specified URL. 
 [Cheerio](https://cheerio.js.org/)- it is a library for parsing and manipulating HTML that is commonly used here for extracting data from downloaded HTML content.
[Apify SDK](https://docs.apify.com/sdk/js/docs/guides/apify-platform)- it is for building Apify Actors, that is utilized for initializing actor environments, getting input data, and pushing extracted data to the dataset.

**Data storage** 
For convenient and scalable storage solutions for extracted information, data is being stored in Apify’s dataset.

**Enhancing development workflows** 
The provided template offers a structured framework and built-in functionality for web scraping tasks, therefore the development process is streamlined. Developers should not worry about the boilerplate setup instead they should focus on customizing scraping logic. 

**Deploying and running the scraper**
It is important to follow steps below for successful deployment and running of the scraper to Apify platform.

1. Create an account on [Apify](https://console.apify.com/sign-up), don’t worry if you have the account already.
2. Login into your account and navigate to the [Actors](https://console.apify.com/actors) section.
Click on “[create new actor](https://console.apify.com/actors/new)” and choose to create an actor from your local code.
3. Upload the provided file containing scraper code.
4. Configure any setting or required environment variable, e.g, input URL.
5. Save changes and deploy the actor.

You can execute the actor deployed on the Apify platform through accessing its detail page and running it manually or scheduling it to run at specified intervals. 

In conclusion, by following steps illustrated in this guide, will enable developers to create sophisticated web scrapers with ease. In addition, developers and organizations can harness the power of web scraping to gather insightful data for their projects.