How to Get Fetching Google Trends Data Using Python

As a developer, I often find myself curious about the popularity of different programming languages and technologies. Thankfully, Google Trends provides a powerful way to analyze search interest over time. In this tutorial, I’ll show you how I fetch Google Trends data using Python with the pytrends library. We’ll start with a basic script and then enhance it by adding user inputs, CSV storage, and visualization using matplotlib.

Breakdown Structure

Install pytrends

Before we get started, we need to install pytrends, which is a Python API for accessing Google Trends data.

pip install pytrends

Import the Required Module

To interact with Google Trends, we need to import TrendReq from pytrends.

from pytrends.request import TrendReq

Initialize a pytrends Request

We need to create a session to fetch data from Google Trends.

pytrends = TrendReq()

Define Keywords

We specify the keywords we want to track over time.

keywords = ["Python", "Java", "C++"]

Build the Payload (Query Parameters)

We prepare the request by defining our keywords and the timeframe.

pytrends.build_payload(keywords, timeframe="today 12-m")
  • timeframe="today 12-m": Fetches data for the past 12 months.

Fetch Interest Over Time

Now, we retrieve historical search trends for the given keywords.

data = pytrends.interest_over_time()

Display the Data

Finally, we print the retrieved data to see the trends.

print(data)

Sample Output:

date        Python  Java  C++  isPartial
2023-12-24  22      16    86   False
2023-12-31  25      17    86   False
2024-01-07  30      19    94   False
2024-01-14  31      20    95   False
2024-01-21  33      20    95   False

Enhancing the Code with Practical Functionality

While the basic script works fine, I wanted to add more functionality:

  • Allow user input for keywords and timeframe
  • Save the data as a CSV file
  • Visualize the trends using Matplotlib

Updated Code with Enhancements

import pandas as pd
import matplotlib.pyplot as plt
from pytrends.request import TrendReq

# Initialize pytrends session
pytrends = TrendReq()

# Get user input for keywords
keywords = input("Enter keywords separated by commas: ").split(",")

# Trim whitespace from keywords
keywords = [kw.strip() for kw in keywords]

# Get user input for timeframe
timeframe = input("Enter timeframe (e.g., 'today 12-m', 'today 5-y', '2023-01-01 2024-01-01'): ")

# Build payload with user-defined keywords and timeframe
pytrends.build_payload(keywords, timeframe=timeframe)

# Fetch interest over time
data = pytrends.interest_over_time()

# Remove 'isPartial' column if it exists
if 'isPartial' in data.columns:
    data = data.drop(columns=['isPartial'])

# Save data to CSV file
csv_filename = "google_trends_data.csv"
data.to_csv(csv_filename)

# Display data
print(f"Data saved to {csv_filename}")
print(data)

# Plot the trends
plt.figure(figsize=(10, 5))
for keyword in keywords:
    plt.plot(data.index, data[keyword], label=keyword)

plt.xlabel("Date")
plt.ylabel("Search Interest")
plt.title("Google Trends Interest Over Time")
plt.legend()
plt.xticks(rotation=45)
plt.grid()

# Show the plot
plt.show()

Key Features of the Enhanced Script

User Input for Keywords & Timeframe

The script now asks for keywords and timeframe dynamically, making it flexible and interactive.

Saves Data to a CSV File

The trends data is saved as google_trends_data.csv for future analysis.

Plots the Trends Using Matplotlib

We can now visualize the trends with a line chart to understand search interest fluctuations over time.

Example Output

Terminal Output:

Enter keywords separated by commas: Python, Java, C++
Enter timeframe (e.g., 'today 12-m', 'today 5-y', '2023-01-01 2024-01-01'): today 5-y
Data saved to google_trends_data.csv

Graph Output: A line chart displaying search trends of “Python”, “Java”, and “C++” over time.

Final Thoughts

This enhanced version of our script makes it much more useful! Now, we can dynamically select keywords, define custom timeframes, save data for later use, and visualize insights with graphs. This is a great tool for analyzing search trends in various domains, including technology, finance, marketing, and beyond.

Related blog posts