DEV Community

Sophia
Sophia

Posted on

A Simple Python Script for Tracking Keyword Search Volume

I've been building a simple dashboard to track keyword rankings for my clients, and I wanted to share a Python snippet that pulls search volume data from Google Trends. It's great for quick analysis without needing a paid API.

python
import pandas as pd
from pytrends.request import TrendReq

def get_search_volume(keywords, timeframe='today 3-m'):
pytrends = TrendReq(hl='en-US', tz=360)
pytrends.build_payload(keywords, cat=0, timeframe=timeframe, geo='', gprop='')
data = pytrends.interest_over_time()
if not data.empty:
data = data.drop(columns=['isPartial'])
return data.mean()
return pd.Series(dtype=float)

keywords = ['SEO tools', 'local SEO', 'keyword research']
volumes = get_search_volume(keywords)
print(volumes)

This gives you average relative search volume over the last 3 months. For more accurate, real-time data, I've been using SERPSpur's API to get precise numbers. But for free, this works well enough. What's your preferred method for keyword volume estimation?

Top comments (0)