DEV Community

Cover image for Scrape Google Trends Trending Now in Javascript
Terry Tan
Terry Tan

Posted on

Scrape Google Trends Trending Now in Javascript

We have already covered scraping Google Trends Trending Now in Python and if you are looking to extract data from the 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' Trending Now

Google Trends Daily Search Trends

Google Trends Realtime Search Trends

Google Trends Explore allow you to get data for specific query or compare trends between queries. As for Google Trends’ Trending Now, it shows the top queries for the day from 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 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 take care of everything mentioned above with fast response times under ~2.47 seconds (~1.33 seconds with Ludicrous speed) per request. The result is a well structured data in JSON with only single API call.

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

SerpApi's 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 line 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)
Enter fullscreen mode Exit fullscreen mode

Prerequisite

Install library:

npm install serpapi
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Once parameters is defined, everything else will be taken care 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 it to response.

const response = await getJson(params)
Enter fullscreen mode Exit fullscreen mode

Output:

console.log(response)
Enter fullscreen mode Exit fullscreen mode
"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"
}
Enter fullscreen mode Exit fullscreen mode

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 mean you reach the end of 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
})
Enter fullscreen mode Exit fullscreen mode

Other Parameters

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

Daily Search Trends in France

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

Daily Search Trends in France at 1st of October 2023

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

Realtime Search Trends in United States

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

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"
}
Enter fullscreen mode Exit fullscreen mode

Next page results of the 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=="
}
Enter fullscreen mode Exit fullscreen mode

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 compare to your typical web scraping experience. Alternatively, you can checkout pytrends github repository which is a popular Python open source google trends scraper.

Top comments (0)