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 demos 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 Explore allows you to get data for specific queries or compare trends between queries. As for Google Trends’ Trending Now, it shows the top queries for the day from the 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 a well structured data in JSON with only 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 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)
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 it to 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
returned 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 results when there is no next_date
returned 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.
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 that 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.
If you have any questions, please feel free to reach out to me.
Add a Feature Request💫 or a Bug🐞