Skip to main content

Scrape Search Data with SERP API using Node.js

In our previous tutorial, you have learned about Create RESTful API with Node.js, Express & MongoDB. In this tutorial, you will learn how to get the SERP data using SERP API with Node.js.

SERP or Search Engine Results Pages is the result pages data that returned by Search Engines. The search results pages data are relevant to the search keywords that generally done manually by the users in Search Engines.

Sometimes we needs search result pages data from all result pages and we develop algorithm for this. But it’s not always accurate as the Search Engines regularly keeps on changing their SERP structures and algorithms. Now we don’t need to be panic as there are SERP APIs are available. The SERP APIs are reliable and allows us to scrape accurate search result pages data.

So if you’re running a business or a developer and looking for accurate SERP data, then you’re at the right place.

The SerpApi is a secure Google Search API that allows to scrape search engine (Google, Bing, etc.) results from anywhere in the world with location parameter and returns response data into JSON data. It can also be easily consumed with any programming language (PHP, Python, Ruby, Nodejs, etc.).

Also, read:

So let’s proceed with the tutorial to integrate SERP API with Node.js.

Step1: Get API Access Key

The API Key is required to to access the API. So first we will create an account and get the API Access Key from dashbaord.

Step2: Install SerpApi NodeJS Module

We will install the Google Search Results Node.js module with following command.

$ npm install google-search-results-nodejs

After module installation, we will include module into our scipt.

const GSR = require('google-search-results-nodejs')

Step3: Use API Access Key

We will pass API Access Key to Google Search Results Node module object to create the client to access the API.

let client = new GSR.GoogleSearchResults("API_ACCESS_KEY")

Step4: Build Search Query

We will create search query with parameters and pass values.

queryParams = {
    q: "Coffee",
    location: "Austin, Texas, United States",
    hl: "en",
    gl: "us",
    engine: "google",
    google_domain: "google.com",
}

Step5: Make API Request

We will make API request by passing parameter and callback to display result data. So first we will create callback function.

var callback = function(data) {
  console.log(data)
}

Now we will make API request by passing search query. The results data will be displayed through callback function.

client.json(queryParams, callback)

Step6: Complete Code

Here is the complete code to make SERP API request and get response result data into JSON data.


const GSR = require('google-search-results-nodejs')
let client = new GSR.GoogleSearchResults("API_ACCESS_KEY")

queryParams = {
	q: "Coffee",
    location: "Austin, Texas, United States",
    hl: "en",
    gl: "us",
	engine: "google",
    google_domain: "google.com",
}

var callback = function(data) {
  console.log(data)
}

client.json(queryParams, callback)

In this tutorial, we have explained how to integrate SerpApi with Node.js to scrape the search result data. Here we have created a simple script with some required parameters to make API request and get result data into JSON data. You can checkout the API Documentation for advanced options and customize the search according to your requirement.

You may also like: