Skip to main content

Modern Google Trends API - Combining the best of pytrends, with RSS feeds, Selenium scraping, DrissionPage browser automation, and enhanced features

Project description

pytrends-modern

The Modern Google Trends API - Combining the best features from pytrends, trendspyg, and more.

Python 3.8+ License: MIT

๐ŸŒŸ Why pytrends-modern?

pytrends-modern is a next-generation Google Trends library that combines:

  • โœ… All classic pytrends features - Interest over time, by region, related topics/queries
  • โœ… RSS Feed Support - Fast real-time trending data with rich media (0.2s vs 10s)
  • โœ… Enhanced Error Handling - Automatic retries, rate limit management, proxy rotation
  • โœ… Modern Python - Full type hints, async support, dataclasses
  • โœ… Selenium Integration - Advanced scraping when needed
  • โœ… Multiple Export Formats - CSV, JSON, Parquet, Excel, DataFrame
  • โœ… Comprehensive CLI - Easy command-line interface with rich output
  • โœ… Better Rate Limiting - Smart backoff, quota management
  • โœ… Active Maintenance - Modern codebase, actively maintained

๐Ÿš€ Quick Start

Installation

# Basic installation
pip install pytrends-modern

# With browser mode (Camoufox for bypassing rate limits)
pip install pytrends-modern[browser]

# With Selenium support (for advanced scraping)
pip install pytrends-modern[selenium]

# With CLI support
pip install pytrends-modern[cli]

# With all features
pip install pytrends-modern[all]

Basic Usage

from pytrends_modern import TrendReq

# Initialize
pytrends = TrendReq(hl='en-US', tz=360)

# Build payload
pytrends.build_payload(
    kw_list=['Python', 'JavaScript'],
    timeframe='today 12-m',
    geo='US'
)

# Get interest over time
interest_df = pytrends.interest_over_time()
print(interest_df.head())

# Get interest by region
region_df = pytrends.interest_by_region()
print(region_df.head())

# Get related queries
related = pytrends.related_queries()
print(related['Python']['top'])

๐ŸฆŠ Browser Mode (Camoufox) - Bypass Rate Limits

NEW! Use Camoufox with advanced fingerprinting to bypass Google's rate limits by using your Google account:

from pytrends_modern import TrendReq, BrowserConfig
from pytrends_modern.camoufox_setup import setup_profile

# First-time setup: Configure Google account login
setup_profile()  # Opens browser - log in to Google once

# Use browser mode (persistent login, no rate limits!)
config = BrowserConfig(headless=False)
pytrends = TrendReq(browser_config=config)

# Works like normal API
pytrends.kw_list = ['Python']
df = pytrends.interest_over_time()
print(df.head())

Browser Mode Limitations:

  • โš ๏ธ Only 1 keyword at a time (no comparisons)
  • โš ๏ธ Only 'today 1-m' timeframe
  • โš ๏ธ Only WORLDWIDE region
  • โœ… No rate limits (uses your Google account)
  • โœ… Perfect anti-detection with Camoufox fingerprinting

Setup from command line:

# Check profile status
python -m pytrends_modern.camoufox_setup status

# Run setup (opens browser for Google login)
python -m pytrends_modern.camoufox_setup

# Export profile for Docker/other machines
python -m pytrends_modern.camoufox_setup export camoufox-profile.tar.gz

# Import profile on another machine
python -m pytrends_modern.camoufox_setup import camoufox-profile.tar.gz

Docker Usage:

Yes! You can export your profile and use it in Docker containers:

# 1. Export profile locally
python -m pytrends_modern.camoufox_setup export profile.tar.gz

# 2. Use in Dockerfile
COPY profile.tar.gz /tmp/
RUN mkdir -p /root/.config && \
    cd /root/.config && \
    tar -xzf /tmp/profile.tar.gz

# 3. Use headless="virtual" in container
config = BrowserConfig(headless="virtual")  # Use Xvfb for Docker

Headless Options:

  • headless=False - Show browser window (local development)
  • headless=True - Standard headless (servers with display)
  • headless="virtual" - Xvfb virtual display (Docker containers)

See Dockerfile.example and examples/example_docker_usage.py for complete Docker setup.

โš ๏ธ Security: Profile contains Google session - keep secure, don't commit to git!

RSS Feed (Fast Real-Time Data)

from pytrends_modern import TrendsRSS

# Get trending searches with rich media
rss = TrendsRSS()
trends = rss.get_trends(geo='US')

for trend in trends:
    print(f"Title: {trend['title']}")
    print(f"Traffic: {trend['traffic']}")
    print(f"Articles: {len(trend['articles'])}")
    print(f"Image: {trend['picture']}")
    print("---")

CLI Usage

# Get interest over time
pytrends-modern interest --keywords "Python,JavaScript" --timeframe "today 12-m"

# Get trending searches
pytrends-modern trending --geo US

# Get RSS feed
pytrends-modern rss --geo US --format json

# Export to CSV
pytrends-modern interest --keywords "AI" --output trends.csv

๐Ÿ“Š Features Comparison

Feature pytrends trendspyg pytrends-modern
Interest Over Time โœ… โŒ โœ…
Interest by Region โœ… โŒ โœ…
Related Topics/Queries โœ… โŒ โœ…
RSS Feed โŒ โœ… โœ…
Rich Media (Images/Articles) โŒ โœ… โœ…
Selenium Support โŒ โœ… โœ…
Type Hints โŒ โœ… โœ…
Async Support โŒ โŒ โœ…
CLI โŒ โœ… โœ…
Active Maintenance โŒ โœ… โœ…
Auto Retry Partial โœ… โœ…
Multiple Export Formats โŒ โœ… โœ…

๐ŸŽฏ Key Features

1. Classic Trends Data

All the beloved pytrends methods, modernized:

  • interest_over_time() - Historical search interest
  • interest_by_region() - Geographic distribution
  • related_topics() - Related topics
  • related_queries() - Related searches
  • trending_searches() - Current trending searches
  • today_searches() - Daily trends
  • realtime_trending_searches() - Real-time trends
  • suggestions() - Keyword suggestions

2. RSS Feed Support

Fast access to real-time trending data:

  • 0.2 seconds vs 10+ seconds for full scraping
  • Rich media: images, news articles, headlines
  • Perfect for monitoring and journalism
  • Multiple geo support (125+ countries)

3. Enhanced Error Handling

  • Automatic retry with exponential backoff
  • Rate limit detection and management
  • Proxy rotation support
  • Better error messages

4. Modern Python Features

  • Full type hints for IDE support
  • Async/await support for concurrent requests
  • Dataclasses for structured data
  • Modern exception handling

5. Selenium Integration

  • Fallback for advanced scraping needs
  • Handles JavaScript-rendered content
  • Automatic driver management
  • Headless mode support

6. Multiple Export Formats

# Export to various formats
df = pytrends.interest_over_time()

# CSV
df.to_csv('trends.csv')

# JSON
pytrends.to_json('trends.json')

# Parquet (requires pyarrow)
pytrends.to_parquet('trends.parquet')

# Excel (requires openpyxl)
df.to_excel('trends.xlsx')

๐Ÿ“š Documentation

TrendReq Class

The main class for Google Trends API access.

TrendReq(
    hl='en-US',          # Language
    tz=360,              # Timezone offset
    geo='',              # Geographic location
    timeout=(2, 5),      # (connect, read) timeouts
    proxies=None,        # Proxy list or dict
    retries=3,           # Number of retries
    backoff_factor=0.3,  # Backoff multiplier
    verify_ssl=True      # SSL verification
)

Build Payload

pytrends.build_payload(
    kw_list=['keyword1', 'keyword2'],  # Max 5 keywords
    cat=0,                              # Category (0 = all)
    timeframe='today 5-y',             # Time range
    geo='',                            # Geographic location
    gprop=''                           # Property ('', 'images', 'news', 'youtube', 'froogle')
)

Time Frames

  • 'now 1-H' - Last hour
  • 'now 4-H' - Last 4 hours
  • 'now 1-d' - Last day
  • 'now 7-d' - Last 7 days
  • 'today 1-m' - Past 30 days
  • 'today 3-m' - Past 90 days
  • 'today 12-m' - Past 12 months
  • 'today 5-y' - Past 5 years (default)
  • 'all' - Since 2004
  • 'YYYY-MM-DD YYYY-MM-DD' - Custom range

Geographic Codes

Use ISO 3166-1 alpha-2 country codes:

  • 'US' - United States
  • 'GB' - United Kingdom
  • 'US-CA' - California (US states)
  • 'FR' - France
  • etc.

Categories

Common category codes:

๐Ÿ”ง Advanced Usage

Proxy Support

# List of proxies
pytrends = TrendReq(
    proxies=['https://proxy1.com:8080', 'https://proxy2.com:8080'],
    retries=3
)

# Dict format
pytrends = TrendReq(
    proxies={
        'http': 'http://proxy.com:8080',
        'https': 'https://proxy.com:8080'
    }
)

Async Support

import asyncio
from pytrends_modern import AsyncTrendReq

async def get_trends():
    pytrends = AsyncTrendReq(hl='en-US')
    await pytrends.build_payload(['Python', 'JavaScript'])
    df = await pytrends.interest_over_time()
    return df

df = asyncio.run(get_trends())

Rate Limit Handling

from pytrends_modern import TrendReq
from pytrends_modern.exceptions import TooManyRequestsError

pytrends = TrendReq(retries=5, backoff_factor=0.5)

try:
    pytrends.build_payload(['keyword'])
    df = pytrends.interest_over_time()
except TooManyRequestsError:
    print("Rate limited. Wait before retrying.")

Batch Processing

from pytrends_modern import TrendReq
import time

keywords = ['Python', 'JavaScript', 'Rust', 'Go', 'Java']
pytrends = TrendReq()

results = {}
for kw in keywords:
    pytrends.build_payload([kw], timeframe='today 12-m')
    results[kw] = pytrends.interest_over_time()
    time.sleep(2)  # Avoid rate limits

๐Ÿงช Testing

# Run tests
pytest

# With coverage
pytest --cov=pytrends_modern

# Specific test
pytest tests/test_request.py::test_interest_over_time

๐Ÿค Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new features
  4. Ensure all tests pass
  5. Submit a pull request

๐Ÿ“ License

MIT License - see LICENSE file for details

๐Ÿ™ Credits

This project builds upon and combines features from:

๐Ÿ“Š Changelog

Version 1.0.0 (2025-12-26)

  • Initial release
  • Combined pytrends, trendspyg, and google-trends features
  • Added async support
  • Full type hints
  • Enhanced error handling
  • CLI interface
  • Multiple export formats

๐Ÿ“Œ Important Notes

Google API Changes

Google has deprecated several trending search API endpoints. pytrends-modern provides two working alternatives:

Option 1: Fast RSS Feed (Recommended for most use cases)

from pytrends_modern import TrendsRSS

rss = TrendsRSS()
trends = rss.get_trends(geo='US')  # ~0.7s, returns 10 trends with images/articles

Pros: Lightning fast, includes rich media, no browser needed
Cons: Limited to 10 trends, no filtering options

Option 2: Selenium Web Scraper (For complete data)

from pytrends_modern import TrendsScraper

scraper = TrendsScraper(headless=True)
df = scraper.trending_searches(geo='US', hours=24)  # ~15s, returns 400+ trends
scraper.close()

Pros: Complete data (400+ trends), supports categories/filters
Cons: Slower, requires Chrome browser

Working Features

โœ… All core API methods work perfectly:

  • interest_over_time() - Historical search trends
  • interest_by_region() - Geographic distribution
  • related_queries() / related_topics() - Related searches
  • suggestions() - Keyword suggestions
  • And more!

โœ… RSS feeds for 125+ countries
โœ… Selenium scraper for comprehensive trending data

โš ๏ธ Disclaimer

This is an unofficial library and is not affiliated with or endorsed by Google. Use responsibly and in accordance with Google's Terms of Service.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytrends_modern-0.2.2.tar.gz (49.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytrends_modern-0.2.2-py3-none-any.whl (42.8 kB view details)

Uploaded Python 3

File details

Details for the file pytrends_modern-0.2.2.tar.gz.

File metadata

  • Download URL: pytrends_modern-0.2.2.tar.gz
  • Upload date:
  • Size: 49.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pytrends_modern-0.2.2.tar.gz
Algorithm Hash digest
SHA256 d51db7a81189ca07a3b103048e9d98fdc166936128de4af2957f0fd4ae251cef
MD5 556faf618e26802200a5c07dfb40b476
BLAKE2b-256 9f4cff99fa411c5cd4a16e8f9c99047c0debf1d025ec6b58c2f16a8431ddf367

See more details on using hashes here.

File details

Details for the file pytrends_modern-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pytrends_modern-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c58cf18ff243b5ea2fe0e0a237937ee45b58a36645665287f798310026c60e83
MD5 4cd9e475e37fdcfe9199a9a1e6b4391a
BLAKE2b-256 f59b59167ce3295df1651ae2b8e45976b4ee5b1a9010b3549e2b02f1516e41fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page