> ## Content Index
> Fetch the complete content index at: https://serpapi.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Web scraping The Home Depot Search Pages with Nodejs and SerpApi
- URL: https://serpapi.com/blog/web-scraping-the-home-depot-search-with-nodejs/
- Published: 2023-01-10T21:20:25.000Z
- Updated: 2023-03-23T17:17:45.000Z
- Description: A step-by-step tutorial on creating The Home Depot Search web scraper in Nodejs.
- Author: Mikhail Zub
- Tags: The Home Depot, webscraping, NodeJS

## What will be scraped

![what](https://user-images.githubusercontent.com/64033139/207391075-a908e7d2-c873-4ec9-bb77-9c0ce669cfbe.png)

## Why use [The Home Depot API](https://serpapi.com/home-depot-search-api) from SerpApi?

Using API generally solves all or most problems that might be encountered while creating your own parser or crawler. From a webscraping perspective, our API can help to solve the most painful problems:

- Bypass blocks from supported search engines by solving CAPTCHA or IP blocks.
- No need to create a parser from scratch and maintain it.
- Pay for proxies, and CAPTCHA solvers.
- Don't need to use browser automation if there's a need to extract data in large amounts faster.

Head to the [Playground](https://serpapi.com/playground?engine=home%5Fdepot) for a live and interactive demo.

## DIY Preparation

First, we need to create a Node.js\* project and add [npm](https://www.npmjs.com/) packages [serpapi](https://www.npmjs.com/package/serpapi) and [dotenv](https://www.npmjs.com/package/dotenv).

To do this, in the directory with our project, open the command line and enter:

```bash
$ npm init -y

```

And then:

```bash
$ npm i serpapi dotenv

```

\*If you don't have Node.js installed, you can [download it from nodejs.org](https://nodejs.org/en/) and follow the installation [documentation](https://nodejs.dev/learn/introduction-to-nodejs).

- SerpApi package is used to scrape and parse search engine results using SerpApi. Get search results from Google, Bing, Baidu, Yandex, Yahoo, Home Depot, eBay, and more.
- dotenv package is a zero-dependency module that loads environment variables from a `.env` file into `process.env`.

Next, we need to add a top-level "type" field with a value of "module" in our `package.json` file to allow [using ES6 modules in Node.JS](https://nodejs.org/api/packages.html#determining-module-system):

![ES6Module](https://user-images.githubusercontent.com/64033139/206865580-6b5326ee-ff65-4890-876e-78112047a03f.png)

For now, we complete the setup Node.JS environment for our project and move to the step-by-step code explanation.

## DIY Code Explanation

First, we need to import `dotenv` from [dotenv](https://www.npmjs.com/package/dotenv) library, `config` and `getJson` from [serpapi](https://www.npmjs.com/package/serpapi) library, `readline` from [readline](https://nodejs.org/api/readline.html) Node.js built-in library, `stdin` and `stdout` (declare them as `input` and `output`) from [process](https://nodejs.org/api/process.html) Node.js built-in library:

```javascript
import dotenv from "dotenv";
import { config, getJson } from "serpapi";
import readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

```

Then, we apply some config. Call `dotenv` [config()](https://www.npmjs.com/package/dotenv#user-content-config) method, call `readline` [createInterface](https://nodejs.org/api/readline.html#readlinepromisescreateinterfaceoptions) method with parameters `input` and `output` and set it to `rl` constant, set your SerpApi Private API key to global [config](https://github.com/serpapi/serpapi-javascript#configuration) object.

```javascript
dotenv.config();
const rl = readline.createInterface({ input, output });
config.api_key = process.env.API_KEY; //your API key from serpapi.com

```

- `dotenv.config()` will read your `.env` file, parse the contents, assign it to `process.env`, and return an object with a `parsed` key containing the loaded content or an `error` key if it failed.
- `readline.createInterface()` creates a new `readlinePromises.Interface` instance. Once the `readlinePromises.Interface` instance is created, the most common case is to listen for the `'line'` event.
- `config.api_key`allows you to declare a global `api_key` value by modifying the config object.

Next, we write the necessary search parameters. We define search `engine`, how many results we want to receive (`resultsLimit` constant) and `params` object with `q` and `page` parameters for making a request:

📌Note: I specifically made a mistake in the search query to demonstrate how [The Home Depot Spell Check API](https://serpapi.com/home-depot-spell-check) works.

```javascript
const engine = "home_depot"; // search engine
const resultsLimit = 40; // hardcoded limit for demonstration purpose
const params = {
  q: "inverter generatot", // Parameter defines the search query
  page: 1, // Value is used to get the items on a specific page
};

```

You can use the next search params:

- `q` parameter defines the search query. You can use anything that you would use in a regular The Home Depot search.
- `hd_sort` parameter defines results sorted by different options. It can be set to: `top_sellers` (Top Sellers), `price_low_to_high` (Price Low to High), `price_high_to_low` (Price High to Low), `top_rated` (Top Rated), `best_match` (Best Match).
- `hd_filter_tokens` used to pass filter tokens divided by comma. [Filter tokens](https://serpapi.com/home-depot-filtering) can be obtained from API response.
- `delivery_zip` ZIP Postal code. To filter the shipping products by a selected area.
- `store_id` store Id to filter the products by the specific store only.
- `lowerbound` defines lower bound for price in USD.
- `upperbound` defines upper bound for price in USD.
- `nao` defines offset for products result. A single page contains `24` products. First page offset is `0`, second -> `24`, third -> `48` and so on.
- `page` value is used to get the items on a specific page. (e.g., `1` (default) is the first page of results, `2` is the 2nd page of results, `3` is the 3rd page of results, etc.).
- `ps` determines the number of items per page. There are scenarios where Home Depot overrides the ps value. By default Home Depot returns `24` results.
- `no_cache` parameter will force SerpApi to fetch the App Store Search results even if a cached version is already present. A cache is served only if the query and all parameters are exactly the same. Cache expires after 1h. Cached searches are free, and are not counted towards your searches per month. It can be set to `false` (default) to allow results from the cache, or `true` to disallow results from the cache. `no_cache` and `async` parameters should not be used together.
- `async` parameter defines the way you want to submit your search to SerpApi. It can be set to `false` (default) to open an HTTP connection and keep it open until you got your search results, or `true` to just submit your search to SerpApi and retrieve them later. In this case, you'll need to use our [Searches Archive API](https://serpapi.com/search-archive-api) to retrieve your results. `async` and `no_cache` parameters should not be used together. `async` should not be used on accounts with [Ludicrous Speed](https://serpapi.com/plan) enabled.

Next, we declare the function `getSearchRefinement` that gets preliminary results. In this function we return available filters for this search and check if `preResults` has `spelling_fix`, set it to `fixedQuery`, and return it:

```javascript
const getSearchRefinement = async () => {
  const preResults = await getJson(engine, params);
  let fixedQuery;
  if (preResults?.search_information?.spelling_fix) {
    fixedQuery = preResults.search_information.spelling_fix;
  }
  return { query: fixedQuery, filters: preResults.filters };
};

```

Next, we declare the function `applyNewSearchParams` which allows setting fixed query and filters. We need to [destructure](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring%5Fassignment) function arguments object to `query` and `filters`:

```javascript
const applyNewSearchParams = async ({ query, filters }) => {
  ...
};

```

In this function, we need to check if `query` is present, and after that, we print a question about the change search query in the console ([question()](https://nodejs.org/api/readline.html#rlquestionquery-options) method) and wait for the answer from the user. If the user's answer is 'y' we set a new search query to the `params` object:

```javascript
if (query) {
  const answer = await rl.question(`Do you want to change the search query from "${params.q}" to "${query}"? y/n: `);
  if (answer.toLowerCase() === "y") {
    params.q = query;
    console.log(`Now the search query is: "${query}"`);
  } else {
    console.log(`The search query didn't change`);
  }
}

```

Next, we need to ask the same questions about applying received filters, by running each filter result using [for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loop:

```javascript
if (filters) {
  const appliedFilters = [];
  let tokens = "";
  for (const filter of filters) {
    const answer = await rl.question(`Do you want to apply some filter from "${filter.key}" category? y/n: `);
    if (answer.toLowerCase() === "y") {
      for (const filterValue of filter.value) {
        const answer = await rl.question(`Do you want to apply "${filterValue.name}" filter from "${filter.key}" category? y/n: `);
        if (answer.toLowerCase() === "y") {
          tokens += `${filterValue.value},`;
          appliedFilters.push(`${filter.key}: ${filterValue.name}`);
        }
      }
    }
  }
  ...
}

```

Next, we close `readline` stream (`rl.close()` method) and if the user chooses some filter, remove the comma at the end of `tokens` string and set it to the `hd_filter_tokens` value in the `params` object:

```javascript
rl.close();
if (tokens) {
  params.hd_filter_tokens = tokens.slice(0, -1);
}

```

Next, we declare the function `getResults` that gets results from all pages (using pagination) and return it:

```javascript
const getResults = async () => {
  ...
};

```

In this function we need to declare an empty `results` array, then using [while](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while) loop to get `json` with results, add `products` from each page and set next page index (to `params.page` value).

If there are no more results on the page or if the number of received results is more than `resultsLimit` we stop the loop (using [break](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break)) and return an array with results:

```javascript
const results = [];
while (true) {
  const json = await getJson(engine, params);
  if (json.products) {
    results.push(...json.products);
    params.page += 1;
  } else break;
  if (results.length >= resultsLimit) break;
}
return results;

```

And finally, run the `getSearchRefinement` function and using [Promise chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using%5Fpromises#chaining) run `applyNewSearchParams` and `getResults` functions. Then we print all the received information in the console with the [console.dir](https://nodejs.org/api/console.html#consoledirobj-options) method, which allows you to use an object with the necessary parameters to change default output options:

```javascript
getSearchRefinement()
  .then(applyNewSearchParams)
  .then(getResults)
  .then((result) => console.dir(result, { depth: null }));

```

## Output

```json
[
  {
    "position": 1,
    "product_id": "318783386",
    "title": "2500-Watt Recoil Start Ultra-Light Portable Gas and Propane Powered Dual Fuel Inverter Generator with CO Shield",
    "thumbnails": [
      [
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_65.jpg",
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_100.jpg",
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_145.jpg",
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_300.jpg",
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_400.jpg",
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_600.jpg",
        "https://images.thdstatic.com/productImages/95951f39-703a-4efc-b12b-ad3a3276e73c/svn/champion-power-equipment-inverter-generators-201122-64_1000.jpg"
      ],
      [
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_65.jpg",
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_100.jpg",
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_145.jpg",
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_300.jpg",
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_400.jpg",
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_600.jpg",
        "https://images.thdstatic.com/productImages/27667698-e4fc-488f-b713-7f51e83d5b5b/svn/champion-power-equipment-inverter-generators-201122-e4_1000.jpg"
      ]
    ],
    "link": "https://www.homedepot.com/p/Champion-Power-Equipment-2500-Watt-Recoil-Start-Ultra-Light-Portable-Gas-and-Propane-Powered-Dual-Fuel-Inverter-Generator-with-CO-Shield-201122/318783386",
    "serpapi_link": "https://serpapi.com/search.json?delivery_zip=04401&engine=home_depot_product&product_id=318783386&store_id=2414",
    "model_number": "201122",
    "brand": "Champion Power Equipment",
    "collection": "https://www.homedepot.com/collection/Outdoors/Champion-Inverter-Generators-Accessories-Collection/Family-311405669?omsid=318783386",
    "rating": 4.7821,
    "reviews": 1606,
    "price": 899,
    "delivery": {
      "free": true,
      "free_delivery_threshold": false
    },
    "pickup": {
      "free_ship_to_store": true
    }
  }
]

```

## DIY Full Code

If you don't need an explanation, have a look at the [full code example in the online IDE](https://replit.com/@MikhailZub/Scrape-The-Home-Depot-Search-with-NodeJS-SerpApi#index.js)

```javascript
import dotenv from "dotenv";
import { config, getJson } from "serpapi";
import readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

dotenv.config();
const rl = readline.createInterface({ input, output });
config.api_key = process.env.API_KEY; //your API key from serpapi.com

const engine = "home_depot"; // search engine
const resultsLimit = 40; // hardcoded limit for demonstration purpose
const params = {
  q: "inverter generatot", // Parameter defines the search query
  page: 1, // Value is used to get the items on a specific page
};

const getSearchRefinement = async () => {
  const preResults = await getJson(engine, params);
  let fixedQuery;
  if (preResults?.search_information?.spelling_fix) {
    fixedQuery = preResults.search_information.spelling_fix;
  }
  return { query: fixedQuery, filters: preResults.filters };
};

const applyNewSearchParams = async ({ query, filters }) => {
  if (query) {
    const answer = await rl.question(`Do you want to change the search query from "${params.q}" to "${query}"? y/n: `);
    if (answer.toLowerCase() === "y") {
      params.q = query;
      console.log(`Now the search query is: "${query}"`);
    } else {
      console.log(`The search query didn't change`);
    }
  }
  if (filters) {
    const appliedFilters = [];
    let tokens = "";
    for (const filter of filters) {
      const answer = await rl.question(`Do you want to apply some filter from "${filter.key}" category? y/n: `);
      if (answer.toLowerCase() === "y") {
        for (const filterValue of filter.value) {
          const answer = await rl.question(`Do you want to apply "${filterValue.name}" filter from "${filter.key}" category? y/n: `);
          if (answer.toLowerCase() === "y") {
            tokens += `${filterValue.value},`;
            appliedFilters.push(`${filter.key}: ${filterValue.name}`);
          }
        }
      }
    }
    rl.close();
    if (tokens) {
      params.hd_filter_tokens = tokens.slice(0, -1);
    }
  }
};

const getResults = async () => {
  const results = [];
  while (true) {
    const json = await getJson(engine, params);
    if (json.products) {
      results.push(...json.products);
      params.page += 1;
    } else break;
    if (results.length >= resultsLimit) break;
  }
  return results;
};

getSearchRefinement()
  .then(applyNewSearchParams)
  .then(getResults)
  .then((result) => console.dir(result, { depth: null }));

```

## Links

- [Code in the online IDE](https://replit.com/@MikhailZub/Scrape-The-Home-Depot-Search-with-NodeJS-SerpApi#index.js)
- [The Home Depot API](https://serpapi.com/home-depot-search-api)
- [The Home Depot Filtering API](https://serpapi.com/home-depot-filtering)
- [The Home Depot Price Bound API](https://serpapi.com/home-depot-price-bound)
- [The Home Depot Sorting API](https://serpapi.com/home-depot-sorting)
- [The Home Depot Spell Check API](https://serpapi.com/home-depot-spell-check)
- [The Home Depot API Playground](https://serpapi.com/playground?engine=home%5Fdepot)

If you want other functionality added to this blog post or if you want to see some projects made with SerpApi, [write me a message](mailto:miha01012019@gmail.com).

---

Join us on [Twitter](https://twitter.com/serp%5Fapi) | [YouTube](https://www.youtube.com/channel/UCUgIHlYBOD3yA3yDIRhg%5Fmg)

Add a [Feature Request](https://github.com/serpapi/public-roadmap/issues)💫 or a [Bug](https://github.com/serpapi/public-roadmap/issues)🐞