Skip to main content

A Python library for accessing Google Trends data

Project description

TrendsPy

Python library for accessing Google Trends data.

Key Features

Explore

  • Track popularity over time (interest_over_time)
  • Analyze geographic distribution (interest_by_region)
  • Compare interest across different timeframes and regions (multirange support)
  • Get related queries and topics (related_queries, related_topics)

Trending Now

  • Access current trending searches (trending_now, trending_now_by_rss)
  • Get related news articles (trending_now_news_by_ids)
  • Retrieve historical data for 500+ trending keywords with independent normalization (trending_now_showcase_timeline)

Search Utilities

  • Find category IDs (categories)
  • Search for location codes (geo)

Flexible Time Formats

  • Custom intervals: 'now 123-H', 'today 45-d'
  • Date-based offsets: '2024-02-01 10-d'
  • Standard ranges: '2024-01-01 2024-12-31'

Installation

pip install trendspy

Basic Usage

from trendspy import Trends
import pandas as pd

# Initialize the client
tr = Trends()

# Get and visualize interest over time
df = tr.interest_over_time(['python', 'javascript'])
df.plot(title='Python vs JavaScript Interest Over Time', 
        figsize=(12, 6))

# Analyze geographic distribution
geo_df = tr.interest_by_region('python')

# Get related queries
related = tr.related_queries('python')

Advanced Features

Search Categories and Locations

# Find technology-related categories
categories = tr.categories(find='technology')
# Output: [{'name': 'Computers & Electronics', 'id': '13'}, ...]

# Search for locations
locations = tr.geo(find='york')
# Output: [{'name': 'New York', 'id': 'US-NY'}, ...]

# Use in queries
df = tr.interest_over_time(
    'python',
    geo='US-NY',      # Found location ID
    cat='13'          # Found category ID
)

Real-time Trending Searches and News

# Get current trending searches in the US
trends = tr.trending_now(geo='US')

# Get trending searches with news articles
trends_with_news = tr.trending_now_by_rss(geo='US')
print(trends_with_news[0])  # First trending topic
print(trends_with_news[0].news[0])  # Associated news article

# Get news articles for specific trending topics
news = tr.trending_now_news_by_ids(
    trends[0].news_tokens,  # News tokens from trending topic
    max_news=3  # Number of articles to retrieve
)
for article in news:
    print(f"Title: {article.title}")
    print(f"Source: {article.source}")
    print(f"URL: {article.url}\n")

Independent Historical Data for Multiple Keywords

from trendspy import BatchPeriod

# Unlike standard interest_over_time where data is normalized across all keywords,
# trending_now_showcase_timeline provides independent data for each keyword
# (up to 500+ keywords in a single request)

keywords = ['keyword1', 'keyword2', ..., 'keyword500']

# Get independent historical data
df_24h = tr.trending_now_showcase_timeline(
    keywords,
    timeframe=BatchPeriod.Past24H  # 16-minute intervals
)

# Each keyword's data is normalized only to itself
df_24h.plot(
    subplots=True,
    layout=(5, 2),
    figsize=(15, 20),
    title="Independent Trend Lines"
)

# Available time windows:
# - Past4H:  ~30 points (8-minute intervals)
# - Past24H: ~90 points (16-minute intervals)
# - Past48H: ~180 points (16-minute intervals)
# - Past7D:  ~42 points (4-hour intervals)

Geographic Analysis

# Country-level data
country_df = tr.interest_by_region('python')

# State-level data for the US
state_df = tr.interest_by_region(
    'python',
    geo='US',
    resolution='REGION'
)

# City-level data for California
city_df = tr.interest_by_region(
    'python',
    geo='US-CA',
    resolution='CITY'
)

Timeframe Formats

  • Standard API timeframes: 'now 1-H', 'now 4-H', 'today 1-m', 'today 3-m', 'today 12-m'
  • Custom intervals:
    • Short-term (< 8 days): 'now 123-H', 'now 72-H'
    • Long-term: 'today 45-d', 'today 90-d', 'today 18-m'
    • Date-based: '2024-02-01 10-d', '2024-03-15 3-m'
  • Date ranges: '2024-01-01 2024-12-31'
  • Hourly precision: '2024-03-25T12 2024-03-25T15' (for periods < 8 days)
  • All available data: 'all'

Multirange Interest Over Time

Compare search interest across different time periods and regions:

# Compare different time periods
timeframes = [
    '2024-01-25 12-d',    # 12-day period
    '2024-06-20 23-d'     # 23-day period
]
geo = ['US', 'GB']        # Compare US and UK

df = tr.interest_over_time(
    'python',
    timeframe=timeframes,
    geo=geo
)

Note: When using multiple timeframes, they must maintain consistent resolution and the maximum timeframe cannot be more than twice the length of the minimum timeframe.

Proxy Support

TrendsPy supports the same proxy configuration as the requests library:

# Initialize with proxy
tr = Trends(proxy="http://user:pass@10.10.1.10:3128")
# or
tr = Trends(proxy={
    "http": "http://10.10.1.10:3128",
    "https": "http://10.10.1.10:1080"
})

# Configure proxy after initialization
tr.set_proxy("http://10.10.1.10:3128")

Documentation

For more examples and detailed API documentation, check out the Jupyter notebook in the repository: basic_usage.ipynb

License

MIT License - see the LICENSE file for details.

Disclaimer

This library is not affiliated with Google. Please ensure compliance with Google's terms of service when using this library.

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

trendspy-0.1.0.tar.gz (238.5 kB view details)

Uploaded Source

Built Distribution

trendspy-0.1.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file trendspy-0.1.0.tar.gz.

File metadata

  • Download URL: trendspy-0.1.0.tar.gz
  • Upload date:
  • Size: 238.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for trendspy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c668083d96a5b938641aea64793908b1a6ec6601527743d08ffc94b4ec44cde
MD5 5afe5cab1d515db104a3448eace046b9
BLAKE2b-256 c8b1b1dd51b0a8507e6937de594dcb6e3fb4c17f8d2119d535228a4b81b563e8

See more details on using hashes here.

File details

Details for the file trendspy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: trendspy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for trendspy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c885b36f40866f1dc438a6b19bb0e6d42918af93066d42c53f207c4dd0d4e602
MD5 b2a7cb7e4e86fa693174fec691e158b0
BLAKE2b-256 466fac5d58ee615ef4e1a7b8e2d4e5f3bc2af0f9f0210530841d1edd37736b2e

See more details on using hashes here.

Supported by

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