Scrape Google Trends Trending Now in Javascript

Scrape Google Trends Trending Now in Javascript

·

6 min read

We have already covered scraping Google Trends Trending Now in Python and if you are looking to extract data from Google Trends Explore, we already have a blog post that demo it in detail. In this tutorial, we will be using SerpApi's Google Trends Trending Now API to scrape Google Trends Trending Now in Javascript.

Google Trends Explore allows you to get data for a specific query or compare trends between queries. As for Google Trends’ Trending Now, it shows the top queries for the day from a pool of Google Trends data.

There are two parts of Trending Now: the first one is Daily Search Trends, and the second is Real-time Search Trends.

Daily Search Trends focus on a single query and its performance throughout a specific day. Unique to Daily Search Trends, it offers data for the past few weeks or even months. It is suitable if you would like to query historical data of trending searches.

Realtime Search Trends provide a dynamic and up-to-the-minute view of what is currently trending. It usually combines with multiple queries. This section displays topics and search queries as they unfold in real-time. Unlike Daily Search Trends, Real-time Search Trends are not confined to a 24-hour window. They continuously update as new trends emerge and fade, making them ideal for tracking breaking news, sports events, and social media trends as they happen.

In summary, the main difference between Daily Search Trends and Real-time Search Trends lies in their time frames and purposes. Daily Search Trends provide a retrospective view of the previous 24 hours or even weeks, making them suitable for analyzing what was trending on a specific day. On the other hand, Real-time Search Trends offer a dynamic, live view of what is currently trending, making them ideal for staying up to date with rapidly evolving events and topics.

Google Trends’ Trending Now can be a valuable tool for various use cases including content creation, news, journalism, product research, marketing and SEO, trend analysis, location-based insights and many others.

Why use an API?

  • No need to create a parser from scratch and maintain it.

  • Bypass blocks from Google: solve CAPTCHA or solve IP blocks.

  • No need to pay for proxies, and CAPTCHA solvers.

  • Don't need to use browser automation.

SerpApi takes care of everything mentioned above with fast response times under ~2.47 seconds (~1.33 seconds with Ludicrous speed) per request. The result is well-structured data in JSON with only a single API call.

Response times and success rates are shown under the SerpApi Status page.

Full code

If you don't need an explanation, feel free to copy and paste the code into your favorite IDE and start running. Otherwise, you can follow the step-by-step guide code explanation to know what each of the lines is doing. You can also check out our playground, which is very handy to play around with the API and get a sense of how things work.

import { getJson } from "serpapi"

const SERPAPI_KEY = "..." // Get your API_KEY from https://serpapi.com/manage-api-key

const params = {
  api_key: SERPAPI_KEY,
  engine: "google_trends_trending_now",
  frequency: "daily", // daily or realtime
  geo: "US",
  hl: "en"
}

const response = await getJson(params)

Prerequisite

Install library:

npm install serpapi

serpapi is an official SerpApi API package. Follow this guide to get your Node.js and npm install if you haven’t done so.

Code Explanation

First, we need to import the library

import { getJson } from "serpapi"

Next, we set the required parameters. A list of parameters and their detailed explanation can be found on our documentation.

const params = {
  api_key: SERPAPI_KEY, // https://serpapi.com/manage-api-key
  engine: "google_trends_trending_now", // Make sure the right engine is set
  frequency: "daily", // daily or realtime
  geo: "US", // Location to search for
  hl: "en" // Language of the results
}

Once parameters are defined, everything else will be taken care of by the library, for example, making the actual API request. Calling getJson to make the API request and JSON data will be returned and assigned to the response.

const response = await getJson(params)

Output:

console.log(response)
"daily_searches": [
  {
    "date": "20231025",
    "searches": [
      {
        "query": "Maine shooting",
        "google_trends_link": "https://trends.google.com/trends/explore?q=Maine+shooting&date=now+7-d&geo=US",
        "serpapi_google_trends_link": "https://serpapi.com/search.json?data_type=TIMESERIES&date=now+7-d&engine=google_trends&geo=US&q=Maine+shooting&tz=420",
        "traffic": 5000000,
        "related_queries": [
          {
            "query": "lewiston maine",
            "google_trends_link": "https://trends.google.com/trends/explore?q=lewiston+maine&date=now+7-d&geo=US",
            "serpapi_google_trends_link": "https://serpapi.com/search.json?data_type=TIMESERIES&date=now+7-d&engine=google_trends&geo=US&q=lewiston+maine&tz=420"
          },
          ...
        ],
        "articles": [
          {
            "title": "Manhunt for Maine shooting suspect enters second day",
            "link": "https://www.bbc.com/news/world-us-canada-67232966",
            "snippet": "Hundreds of law enforcement personnel are part of the search as towns remain under shelter in place orders.",
            "source": "BBC News",
            "date": "1h ago",
            "thumbnail": "https://t1.gstatic.com/images?q=tbn:ANd9GcR-vgroBO4SB7JAB8HE4J8fOBmF01lY6K-HUBMB0Vbc2lfIGqc_p3qDuE6wuZPTXjIEEBycQD0O"
          },
          ...
        ]
      },
      {
        "query": "Celtics",
        "google_trends_link": "https://trends.google.com/trends/explore?q=Celtics&date=now+7-d&geo=US",
        "serpapi_google_trends_link": "https://serpapi.com/search.json?data_type=TIMESERIES&date=now+7-d&engine=google_trends&geo=US&q=Celtics&tz=420",
        "traffic": 200000,
        "related_queries": [
          {
            "query": "Knicks",
            "google_trends_link": "https://trends.google.com/trends/explore?q=Knicks&date=now+7-d&geo=US",
            "serpapi_google_trends_link": "https://serpapi.com/search.json?data_type=TIMESERIES&date=now+7-d&engine=google_trends&geo=US&q=Knicks&tz=420"
          },
          ...
        ],
        "articles": [
          {
            "title": "Kristaps Porzingis scores 30, seals win at MSG in Celtics debut ...",
            "link": "https://www.espn.com/nba/story/_/id/38746584/kristaps-porzingis-scores-30-seals-win-vs-knicks-celtics-debut",
            "snippet": "Kristaps Porzingis set a franchise record with 30 points in his Celtics debut and took over down the stretch to lead his new team past his old one Wednesday ...",
            "source": "ESPN",
            "date": "22h ago",
            "thumbnail": "https://t0.gstatic.com/images?q=tbn:ANd9GcTdwRxz8dK0cXCRMX35ovYaRffEbtVoTvpFbfmsYuCC7K76Y5DSnOKKJKi4xE24vqwFFXbs7A6h"
          },
          ...
        ]
      },
      {
        "query": "Spurs",
        "google_trends_link": "https://trends.google.com/trends/explore?q=Spurs&date=now+7-d&geo=US",
        "serpapi_google_trends_link": "https://serpapi.com/search.json?data_type=TIMESERIES&date=now+7-d&engine=google_trends&geo=US&q=Spurs&tz=420",
        "traffic": 200000,
        "related_queries": [
          {
            "query": "Wembanyama",
            "google_trends_link": "https://trends.google.com/trends/explore?q=Wembanyama&date=now+7-d&geo=US",
            "serpapi_google_trends_link": "https://serpapi.com/search.json?data_type=TIMESERIES&date=now+7-d&engine=google_trends&geo=US&q=Wembanyama&tz=420"
          },
          ...
        ],
        "articles": [
          {
            "title": "Victor Wembanyama finishes with 15 points in debut as Spurs lose ...",
            "link": "https://www.espn.com/nba/story/_/id/38746746/victor-wembanyama-15-points-debut-spurs-lose",
            "snippet": "SAN ANTONIO -- As Victor Wembanyama walked on to the Frost Bank Center court during pregame warmups for his NBA regular-season debut, he spread his arms ...",
            "source": "ESPN",
            "date": "15h ago",
            "thumbnail": "https://t0.gstatic.com/images?q=tbn:ANd9GcSlm55dFiq8eyKV7otfGAXOGknwusoMZamiiIPvtiyiGcLfQ--O9_6DErU1V8oAwS1jguLgXnlD"
          },
          ...
        ]
      },
      ...
    ]
  }
],
"serpapi_pagination": {
  "current_date": "20231025",
  "next_date": "20231024",
  "next": "https://serpapi.com/search.json?date=20231024&engine=google_trends_trending_now&frequency=daily&geo=US&hl=en"
}

Next Date Result

Similar code but to get the next date result, we have to get and set the date with next_date return by the first API call. You can get the next date result as long as the API is returning next_date, which means you reach the end of the result when there is no next_date return from the API.

const nextDate = response["serpapi_pagination"]["next_date"]
const nextDateResponse = await getJson({
  ...params,
  date: nextDate
})

Other Parameters

Here is a list of combinations of parameters that you can use to get different results. api_key is omitted for simplicity.

const params = {
  engine: "google_trends_trending_now",
  frequency: "daily",
  geo: "FR",
  hl: "en"
}

Daily Search Trends in France

const params = {
  engine: "google_trends_trending_now",
  frequency: "daily",
  date: "20231001",
  geo: "FR",
  hl: "en"
}

Daily Search Trends in France on 1st of October 2023

const params = {
  engine: "google_trends_trending_now",
  frequency: "realtime",
  geo: "US",
  hl: "en"
}

Realtime Search Trends in the United States

const params = {
  engine: "google_trends_trending_now",
  frequency: "realtime",
  geo: "US",
  hl: "en",
  cat: "s"
}

Realtime Search Trends in the United States for the Sports category

const params = {
  engine: "google_trends_trending_now",
  frequency: "realtime",
  geo: "US",
  hl: "en",
  cat: "s",
  next_page_token: "WyJVU19sbmtfNnNEUm1BQUFBQUE3Mk1fZW4iLCJVU19sbmtfSHFrZ2xnQUFBQUEtdk1fZW4iLCJVU19sbmtfbnhqSWx3QUFBQUJYRE1fZW4iLCJVU19sbmtfOC1iWW1BQUFBQUFyX01fZW4iLCJVU19sbmtfMUl2TGx3QUFBQUFmbk1fZW4iLCJVU19sbmtfWnozLW1BQUFBQUNaSk1fZW4iLCJVU19sbmtfb1E0Qm1RQUFBQUNnRk1fZW4iLCJVU19sbmtfeUVmc21BQUFBQUFrWE1fZW4iLCJVU19sbmtfWF9uZ2x3QUFBQUNfN01fZW4iLCJVU19sbmtfR3BWcG1BQUFBQUJ6ak1fZW4iLCJVU19sbmtfLXpmdW1BQUFBQUFWTE1fZW4iLCJVU19sbmtfZkVqYmx3QUFBQUNuWE1fZW4iLCJVU19sbmtfMzVtRmx3QUFBQUJhak1fZW4iLCJVU19sbmtfWWdSS21BQUFBQUFvSE1fZW4iLCJVU19sbmtfTGlMMW1BQUFBQURiT01fZW4iLCJVU19sbmtfODFBSm1BQUFBQUQ2U01fZW4iLCJVU19sbmtfZDY1NW1BQUFBQUFPdE1fZW4iLCJVU19sbmtfNHhMcGx3QUFBQUFLQk1fZW4iLCJVU19sbmtfV3VVdGx3QUFBQUIzOE1fZW4iLCJVU19sbmtfTUJIem1BQUFBQUREQ01fZW4iXQ=="
}

Next page results of the Realtime Search Trends in the United States for the Sports category

Documentation

Conclusions

Google Trends' Trending Now is a great data source for understanding the events happening in real-time or in the past. SerpApi provides a simple yet powerful solution to scrape those data without any headache on blocking and maintenance as compared to your typical web scraping experience. Alternatively, you can checkout pytrends Github repository which is a popular Python open source google trends scraper.