A Python package to collect and analyze Google Trends data for US states.
Project description
GTUS - Google Trends for US States
gtusis a Python package that enables users to collect and analyze Google Trends data across various US states. This package is designed to handle multiple queries, manage API delays, and support exporting data to multiple formats, all while being user-friendly and versatile.
Features
-
Collect Google Trends Data:
- Fetch state-level data for multiple queries simultaneously.
- Collect data for all US states or specific states.
- Customize the timeframe for data collection.
-
Error Handling and Retry Logic:
- Built-in retry mechanism with exponential backoff to handle rate-limiting errors (
Too Many Requests).
- Built-in retry mechanism with exponential backoff to handle rate-limiting errors (
-
Data Export Options:
- Save data in Excel format, with each state’s data in separate sheets.
- Export data to a JSON file.
- Create a consolidated DataFrame for advanced analysis.
-
Asynchronous Functionality:
- Collect data asynchronously for faster execution.
Installation
Install GTUS via pip:
pip install gtus
Getting Started
Here's how to start using gtus to collect Google Trends data.
Usage Examples
1. Collect Data for All States
If no states are specified, GTUS will automatically collect data for all US states:
from gtus import GTUS
queries = ["telemedicine", "remote work"]
# Initialize GTUS object without specifying states
gtus = GTUS(queries=queries, timeframe="2022-01-01 2023-01-01")
# Collect data
gtus.collect_all_trends()
# Export to Excel
gtus.export_to_excel("google_trends_all_states.xlsx")
# Export to JSON
gtus.export_to_json("google_trends_all_states.json")
# Create a consolidated DataFrame
dataframe = gtus.create_consolidated_dataframe()
print(dataframe.head())
2. Fetch Data for a Specific State and Query
You can specify a subset of states and queries:
states = ["CA", "NY", "TX"]
queries = ["remote work", "telehealth"]
# Initialize GTUS object
gtus = GTUS(queries=queries, states=states, timeframe="2022-01-01 2023-01-01")
3. Asynchronous Data Collection
from gtus import AsyncGTUS
import asyncio
async def fetch_async():
queries = ["online learning", "work from home"]
# Initialize AsyncGTUS object
async_gtus = AsyncGTUS(queries=queries, timeframe="2022-01-01 2023-01-01")
await async_gtus.collect_all_trends_async()
# Export to Excel and JSON
async_gtus.export_to_excel("async_google_trends.xlsx")
async_gtus.export_to_json("async_google_trends.json")
asyncio.run(fetch_async())
4. Consolidated DataFrame
Combine all collected data into a single DataFrame:
consolidated_df = gtus.create_consolidated_dataframe()
print(consolidated_df.head())
Advanced Configuration
Customize Timeframes
You can specify custom timeframes for data collection:
gtus = GTUS(queries=["climate change"], states=["WA"], timeframe="2010-01-01 2020-12-31")
Adjust Retry Settings
Customize retry attempts and delays for handling rate limits:
gtus = GTUS(queries=["data science"], states=["NY"], delay=10)
result = gtus.fetch_state_trends(query="data science", state="NY", max_retries=5, backoff_factor=2)
Dependencies
pandaspytrendsaiohttp
Contributing
Contributions are welcome! Please submit issues or pull requests via GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for more information.
Start exploring Google Trends data with GTUS today!
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gtus-0.1.0.tar.gz.
File metadata
- Download URL: gtus-0.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77298598c185fc4cadd3de3c3efd8a18df9af980282e2c7353fda0d95c168ecd
|
|
| MD5 |
ba63a10d993629001268978980ddbc4a
|
|
| BLAKE2b-256 |
2e140cfdf4735d2eb2714595f0dbfccf12444fbd190304d3cf80c4130190d04a
|
File details
Details for the file gtus-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gtus-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ee45e4c10ef65bf7cd7a4cd46b41ff0047d56a9718f91b76b73494bd72ef351
|
|
| MD5 |
86f33cfbb3a7935e39e84ec12615bd44
|
|
| BLAKE2b-256 |
f6dbf96bcd1fefb9783ceb0577a3b4bcda12563decd0886b5fab25c3d11bc19a
|