How to scrape Google Play Apps with Node.js
Tags: serpapi, webscraping, node, google
Published: 2022-05-25 07:54:58

<h2 id='what'>What will be scraped</h2>

![what](https://user-images.githubusercontent.com/78694043/191667328-62b03109-05c9-463c-9879-3fbbb876cfd4.png)

📌Note: in this blog post I don't show how to scroll the page, because on this page Google uses infinite scroll, which works with Javascript, so if you want to scroll the page you need to use some browser automation(e.g. Puppeteer) which is much slower.

<h2 id='full_code'>Full code</h2>

If you don't need an explanation, have a look at [the full code example in the online IDE](https://replit.com/@MikhailZub/Google-Play-Apps-scrape-NodeJS-SerpApi#withCheerio.js)

```javascript
const cheerio = require("cheerio");
const axios = require("axios");

const AXIOS_OPTIONS = {
  headers: {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36",
  }, // adding the User-Agent header as one way to prevent the request from being blocked
  params: {
    hl: "en", // Parameter defines the language to use for the Google search
    gl: "us", // parameter defines the country to use for the Google search
  },
};

function getMainPageInfo() {
  return axios.get(`https://play.google.com/store/apps`, AXIOS_OPTIONS).then(function ({ data }) {
    let $ = cheerio.load(data);

    const mainPageInfo = Array.from($(".oVnAB").closest("section")).reduce((result, block) => {
      const categoryTitle = $(block).find(".oVnAB").text().trim();
      if (categoryTitle !== "Top charts") {
        const apps = Array.from($(block).find(".ULeU3b")).map((app) => {
          const link = `https://play.google.com${$(app).find(".Si6A0c").attr("href")}`;
          const appId = link.slice(link.indexOf("?id=") + 4);
          return {
            title: $(app).find(".Epkrse").text().trim(),
            link,
            rating: parseFloat($(app).find(".vlGucd > div:first-child").attr("aria-label").slice(6, 9)),
            thumbnail: $(app).find(".TjRVLb img").attr("src"),
            appId,
          };
        });
        return {
          ...result,
          [categoryTitle]: apps,
        };
      }
    }, {});

    return mainPageInfo;
  });
}

getMainPageInfo().then((result) => console.dir(result, { depth: null }));
```

<h2 id='preparation'>Preparation</h2>

First, we need to create a Node.js\* project and add [`npm`](https://www.npmjs.com/) packages [`cheerio`](https://www.npmjs.com/package/cheerio) to parse parts of the HTML markup, and [`axios`](https://www.npmjs.com/package/axios) to make a request to a website.

To do this, in the directory with our project, open the command line and enter `npm init -y`, and then `npm i cheerio axios`.

\*<span style="font-size: 15px;">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).</span>

<h2 id='process'>Process</h2>

First of all, we need to extract data from HTML elements. The process of getting the right CSS selectors is fairly easy via [SelectorGadget Chrome extension](https://selectorgadget.com/) which able us to grab CSS selectors by clicking on the desired element in the browser. However, it is not always working perfectly, especially when the website is heavily used by JavaScript.

We have a dedicated [web Scraping with CSS Selectors](https://serpapi.com/blog/web-scraping-with-css-selectors-using-python/#css_gadget) blog post at SerpApi if you want to know a little bit more about them.

The Gif below illustrates the approach of selecting different parts of the results.

![how](https://user-images.githubusercontent.com/64033139/190602797-73da63f5-bd28-42bc-95bd-a413e28cbb58.gif)

<h3 id='code_explanation'>Code explanation</h3>

Declare constants from [`cheerio`](https://www.npmjs.com/package/cheerio) and [`axios`](https://www.npmjs.com/package/axios) libraries:

```javascript
const cheerio = require("cheerio");
const axios = require("axios");
```

Next, we write a request options: [`HTTP headers`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) with [`User-Agent`](https://developer.mozilla.org/en-US/docs/Glossary/User_agent) (is used to act as a "real" user visit. [Default `axios` request user-agent is `axios/<axios_version>`](https://github.com/axios/axios/blob/892c241773e7dda78a969ac1faa9b365e24f6cc8/lib/adapters/http.js#L224) so websites understand that it's a script that sends a request and might block it. [Check what's your user-agent](https://www.whatismybrowser.com/detect/what-is-my-user-agent/)), and the necessary parameters for making a request:

```javascript
const AXIOS_OPTIONS = {
  headers: {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36",
  }, // adding the User-Agent header as one way to prevent the request from being blocked
  params: {
    hl: "en", // Parameter defines the language to use for the Google search
    gl: "us", // parameter defines the country to use for the Google search
  },
};
```

Next, we write a function that makes the request and returns the received data. We received the response from [`axios`](https://www.npmjs.com/package/axios) request that has `data` key that we [destructured](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) and parse it with [`cheerio`](https://www.npmjs.com/package/cheerio):

```javascript
function getMainPageInfo() {
  return axios.get(`https://play.google.com/store/apps`, AXIOS_OPTIONS).then(function ({ data }) {
    let $ = cheerio.load(data);
    ...
  })
}
```

Next, we need to get all HTML elements with `".oVnAB"` selector ([`$()`](https://api.jquery.com/class-selector/) method) and find [`closest`](https://api.jquery.com/closest/) `"section"` from all parents elements. Then we use [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) method (it's allow to make the object with results) to iterate an array that built with [`Array.from()`]() method:

```javascript
const mainPageInfo = Array.from($(".oVnAB").closest("section")).reduce((result, block) => {
      ...
    }, {});

    return mainPageInfo;
```

And finally, we need to get `categoryTitle`, and `title`, `link`, `rating`, `thumbnail` and `appId` (we can cut it from `link` using [`slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) and [`indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) methods) of each app from the selected category (using [`$()`](https://api.jquery.com/class-selector/), [`find()`](https://api.jquery.com/find/), [`attr()`](https://api.jquery.com/attr/) and [`text()`](https://api.jquery.com/text/) methods).

On each itaration step we return previous step result (using [`spread syntax`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)) and add the new category with name from `categoryTitle` constant:

📌Note: in this case, we skip the "Top charts" category, because there are built on the page dynamically via JavaScript, and to get it we need to use some browser automation(e.g. Puppeteer) which is much slower.

```javascript
const categoryTitle = $(block).find(".oVnAB").text().trim();
if (categoryTitle !== "Top charts") {
  const apps = Array.from($(block).find(".ULeU3b")).map((app) => {
    const link = `https://play.google.com${$(app).find(".Si6A0c").attr("href")}`;
    const appId = link.slice(link.indexOf("?id=") + 4);
    return {
      title: $(app).find(".Epkrse").text().trim(),
      link,
      rating: parseFloat($(app).find(".vlGucd > div:first-child").attr("aria-label").slice(6, 9)),
      thumbnail: $(app).find(".TjRVLb img").attr("src"),
      appId,
    };
  });
  return {
    ...result,
    [categoryTitle]: apps,
  };
}
```

Now we can launch our parser:

```bash
$ node YOUR_FILE_NAME # YOUR_FILE_NAME is the name of your .js file
```

<h2 id='output'>Output</h2>

```json
{
   "Popular apps":[
      {
         "title":"WhatsApp Messenger",
         "link":"https://play.google.com/store/apps/details?id=com.whatsapp",
         "rating":4.3,
         "thumbnail":"https://play-lh.googleusercontent.com/bYtqbOcTYOlgc6gqZ2rwb8lptHuwlNE75zYJu6Bn076-hTmvd96HH-6v7S0YUAAJXoJN=s256-rw",
         "appId":"com.whatsapp"
      },
      ... and other results
   ],
   "Recommended for you":[
      {
         "title":"Google Earth",
         "link":"https://play.google.com/store/apps/details?id=com.google.earth",
         "rating":4.3,
         "thumbnail":"https://play-lh.googleusercontent.com/9ORDOmn8l9dh-j4Sg3_S7CLcy0RRAI_wWt5jZtJOPztwnEkQ4y7mmGgoSYqbFR5jTc3m=s256-rw",
         "appId":"com.google.earth"
      },
      ... and other results
   ]
   ... and other categories
}
```

<h2 id='serp_api'>Using<a href="https://serpapi.com/google-play-apps"> Google Play Apps Store API </a>from SerpApi</h2>

This section is to show the comparison between the DIY solution and our solution.

The biggest difference is that you don't need to create the parser from scratch and maintain it.

There's also a chance that the request might be blocked at some point from Google, we handle it on our backend so there's no need to figure out how to do it yourself or figure out which CAPTCHA, proxy provider to use.

First, we need to install [`google-search-results-nodejs`](https://www.npmjs.com/package/google-search-results-nodejs):

```bash
npm i google-search-results-nodejs
```

Here's the [full code example](https://replit.com/@MikhailZub/Google-Play-Apps-scrape-NodeJS-SerpApi#withSerpApi.js), if you don't need an explanation:

```javascript
const SerpApi = require("google-search-results-nodejs");
const search = new SerpApi.GoogleSearch(process.env.API_KEY); //your API key from serpapi.com

const params = {
  engine: "google_play", // search engine
  gl: "us", // parameter defines the country to use for the Google search
  hl: "en", // parameter defines the language to use for the Google search
  store: "apps", // parameter defines the type of Google Play store
};

const getJson = () => {
  return new Promise((resolve) => {
    search.json(params, resolve);
  });
};

const getResults = async () => {
  const json = await getJson();
  const appsResults = json.organic_results.reduce((result, category) => {
    const { title: categoryTitle, items } = category;
    const apps = items.map((app) => {
      const { title, link, rating, thumbnail, product_id } = app;
      return {
        title,
        link,
        rating,
        thumbnail,
        appId: product_id,
      };
    });
    return {
      ...result,
      [categoryTitle]: apps,
    };
  }, {});
  return appsResults;
};

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

<h3 id='serp_api_code_explanation'>Code explanation</h3>

First, we need to declare `SerpApi` from [`google-search-results-nodejs`](https://www.npmjs.com/package/google-search-results-nodejs) library and define new `search` instance with your API key from [SerpApi](https://serpapi.com/manage-api-key):

```javascript
const SerpApi = require("google-search-results-nodejs");
const search = new SerpApi.GoogleSearch(API_KEY);
```

Next, we write the necessary parameters for making a request:

```javascript
const params = {
  engine: "google_play", // search engine
  gl: "us", // parameter defines the country to use for the Google search
  hl: "en", // parameter defines the language to use for the Google search
  store: "apps", // parameter defines the type of Google Play store
};
```

Next, we wrap the search method from the SerpApi library in a promise to further work with the search results:

```javascript
const getJson = () => {
  return new Promise((resolve) => {
    search.json(params, resolve);
  });
};
```

And finally, we declare the function `getResult` that gets data from the page and return it:

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

In this function first, we get `json` with results, then we need to iterate `organic_results` array in the received `json`. To do this we use [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) method (it's allow to make the object with results). On each itaration step we return previous step result (using [`spread syntax`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)) and add the new category with name from `categoryTitle` constant:

```javascript
  const json = await getJson();
  const appsResults = json.organic_results.reduce((result, category) => {
    ...
    return {
      ...result,
      [categoryTitle]: apps,
    };
  }, {});
  return appsResults;
```

Next, we destructure `category` element, redefine `title` to `categoryTitle` constant, and itarate the `items` array to get all apps from this category. To do this we need to destructure the `app` element and return this constants:

```javascript
const { title: categoryTitle, items } = category;
const apps = items.map((app) => {
  const { title, link, rating, thumbnail, product_id } = app;
  return {
    title,
    link,
    rating,
    thumbnail,
    appId: product_id,
  };
});
```

After, we run the `getResults` function and 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
getResults().then((result) => console.dir(result, { depth: null }));
```

<h2 id='serp_api_output'>Output</h2>

```json
{
   "Popular apps":[
      {
         "title":"WhatsApp Messenger",
         "link":"https://play.google.com/store/apps/details?id=com.whatsapp",
         "rating":4.3,
         "thumbnail":"https://play-lh.googleusercontent.com/bYtqbOcTYOlgc6gqZ2rwb8lptHuwlNE75zYJu6Bn076-hTmvd96HH-6v7S0YUAAJXoJN=s256-rw",
         "appId":"com.whatsapp"
      },
      ... and other results
   ],
   "Recommended for you":[
      {
         "title":"Gmail",
         "link":"https://play.google.com/store/apps/details?id=com.google.android.gm",
         "rating":4.2,
         "thumbnail":"https://play-lh.googleusercontent.com/KSuaRLiI_FlDP8cM4MzJ23ml3og5Hxb9AapaGTMZ2GgR103mvJ3AAnoOFz1yheeQBBI=s256-rw",
         "appId":"com.google.android.gm"
      },
      ... and other results
   ]
   ... and other categories
}
```

<h2 id='links'>Links</h2>

- [Code in the online IDE](https://replit.com/@MikhailZub/Google-Play-Apps-scrape-NodeJS-SerpApi#index.js)
- [Google Play Apps Store API](https://serpapi.com/google-play-apps)

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

---

<p style="text-align: center;">Join us on <a href="https://twitter.com/serp_api">Twitter</a> | <a href="https://www.youtube.com/channel/UCUgIHlYBOD3yA3yDIRhg_mg">YouTube</a></p>
<p style="text-align: center;">Add a  <a href="https://github.com/serpapi/public-roadmap/issues">Feature Request</a>💫 or a <a href="https://github.com/serpapi/public-roadmap/issues">Bug</a>🐞</p>