How to get the Best Seller Ranking in a few seconds?

The article explains how you can retrieve the Best Seller Ranking, also called the BSR.


The BSR is the list of items categorized by category and famous items that are top sellers!




And now you can improve your business thanks to the BSR and halios-data.


Create an account to RapidAPI

Today, you need to create an account to RAPIDAPI to consume the API, but don't worry. You can use your Google or Github account ;).




Subscribe to the API


After the login, you are generally redirected to the API hub. Else click here

You can test the API for free with 200 requests per month. Click on the subscribe button and choose the Basic plan.















 

Create the project with NestJS

In your terminal, use the following command.


git clone https://github.com/nestjs/typescript-starter.git tutorial-amazon-variant

go to the folder and use npm install



 npm i


After the installation, we can see the following structure.



  • app.controller.ts: we will develop access to our API, i.e. our endpoint;

  • app.module.ts: like the Angular framework, the module represents the partitioning;

  • app.service.ts: we will build our logic of scraping engine consuming and create our custom object.








Let's go to scrape!


 

Implementation of the solution.

In order to know the categories that you can use for the BSR API, you have to consume the category API. For doing this, read the following instructions.


Implementation of service that retrieves the categories


The service aim is to realize the communication with the halios-data API and return the result to the controller.


Now, we implement the communication with the API thanks to RAPIDAPI. To Realize the communication, we propose Axios. You can add it to your package.json with the following command.

npm i -S axios

Below is the communication implementation with RAPIDAPI to get the categories.

To do that, we create a new method in your app.service.ts, getRapidApiOptions.

private getRapidApiOptions(url, params) {
   
  }

We will use this method in order to generate the options needed by RAPIDAPI to consume the API.

The URL is the URL of the endpoint, and the params are the query parameters.

Inside we add the following code :

let options = {
      method: 'GET',
      url: url,
      params: { country: 'US' },
      headers: {
        'X-RapidAPI-Key': 'YOUR_TOKEN',
        'X-RapidAPI-Host': 'amazon24.p.rapidapi.com'
      }
    };
    if (params) {
      options['params'] = params;
    }
    return options;

Don't forget to replace YOUR_TOKEN with your RAPIDAPI token. You can find him in the RAPIDAPI HMI.


Now, you will add the method that consumes the getRapidApiOptions to get the categories from the API.


 getCategories() {
    const options =       
this.getRapidApiOptions('https://amazon24.p.rapidapi.com/api/category', null);
    return new Promise((resolve, reject) => {
      axios.request(options).then(function (response) {
        resolve(response.data);
      }).catch(function (error) {
        reject(error);
      });
    })
  }

The method call the endpoint https://amazon24.p.rapidapi.com/api/category and returns the categories in a promise.


Now we can call our method inside our controller, create our new process inside the app.controller.ts, and call the method that returns the categories.

 @Get('/categories')
  async getCategories() {
    return await this.appService.getCategories();
  }

Let's go to test our endpoint that retrieves the categories. To do this, you can use the POSTMAN app. And add the following URL.

http://localhost:3000/categories



Click on the send button to see the result in a JSON format.



Now, we have to retrieve the categories that are the "isBSRDepartment" attribute equals true and communicate it to a new endpoint to retrieve the BSR.


In our example, you can see that Amazon Devices matches this criterion!.


Implementation of service that retrieves Amazon's BSR


Please return to the app.service.ts and create a new method :

 getBSR(category, params) {

The category is the category ID you have gotten, and the params are the queries you need to communicate to the getRapidOptions () method.


Now, I can propose you the following code,

 getBSR(category, params) {
    const options = this.getRapidApiOptions(`https://amazon24.p.rapidapi.com/api/bsr/${category}`, params);
    return new Promise((resolve, reject) => {
      axios.request(options).then(function (response) {
        resolve(response.data);
      }).catch(function (error) {
        reject(error);
      });
    })
  }

This method reuses the getRapidApiOptions in order to generate the good URL with the good parameters to the RAPIDAPI, and this one returns a result that we encapsulate to a promised solution.


To Finih our implementation, we need to implement the route in the app.controler.ts.

In the API you have two ways to get the BSRs :

  • By the categoryId

  • By the URL

@Get('/bsr/:category')
  async getBSR(@Param() param, @Query('url') url: string, @Query('page') page: string = '1') {

In the signature of the method you can see the possibility to get the elements with the URL or the params with the category attribute.


Below, the implementation.

@Get('/bsr/:category')
  async getBSR(@Param() param, @Query('url') url: string, @Query('page') page: string = '1') {
    const params = { url: '', page: '' };
    if (url) {
      params.url = url;
    }
    return await this.appService.getBSR(param.category, params);
  }

And now you can consume your code with POSTMAN.


With the category ID amazon-devices


Or with the URL

https://www.amazon.com/Best-Sellers-Amazon-Devices-Accessories/zgbs/amazon-devices/ref=zg_bs_nav_0






Now, you can use the API to increase your business.


Thank you and good scraping with us !










26 views0 comments

Discover Halios-Data solution for the web scraping

More Halios-Data

Never miss an update

Thanks for submitting!