A Python package to collect and analyze Google Trends data for US states.
Project description
GTUS - Google Trends for US States
The GTUS package is a Python library designed to collect Google Trends data for specified queries across various U.S. states. It provides functionality to handle multiple queries and states efficiently, while adhering to Google Trends API limitations.
Features
- Collect trends for one or multiple states.
- Batch processing of queries to avoid API limits.
- Adjustable wait time between requests to manage rate limits.
- Export collected data to Excel and JSON formats.
- Access data in a nested dictionary format for easy manipulation in Python.
Installation
To use the GTUS package, ensure you have the required dependencies installed. You can install them using pip:
pip install gtus
This will automatically install all required dependencies, including pytrends, pandas, and aiohttp.
Getting Started
Here's how to start using gtus to collect Google Trends data.
Usage Examples
Importing the Package
To use the GTUS package, import it as follows:
from gtus import GTUS
1. Collect Google Trends Data for US States
Collecting for One State
To collect data for a single state, create an instance of the GTUS class with the desired query and state:
from gtus import GTUS
queries = ["telemedicine", "remote work"]
states = ["TX"]
gtus = GTUS(queries, states, timeframe="2022-01-01 2023-01-01")
# Collect data
gtus.collect_all_trends()
Collecting for Multiple States
To collect data for multiple states, simply provide a list of states:
from gtus import GTUS
queries = ["telemedicine", "remote", "football","dance"]
states = ["CA", "NY", "TX"]
gtus = GTUS(queries, states, timeframe="2020-01-01
2023-01-01")
# Collect data
gtus.collect_all_trends()
Modifying Wait Time
You can adjust the wait time between requests by specifying the wait_time parameter when creating the GTUS instance. This is useful for managing API rate limits:
gtus = GTUS(queries, states, wait_time=15) # Set wait time to 15 seconds
Collecting for All States
If no states are specified, GTUS will automatically collect data for all US states:
queries = ["remote work", "telehealth"]
# Initialize GTUS object
gtus = GTUS(queries=queries, timeframe="2022-01-01 2023-01-01", wait_time=15)
It is strongly recommended to set the wait_time parameter, especially when collecting data for all states or handling a large number of queries. Failing to do so may result in data being collected for only a subset of queries or certain states due to Google's API rate limits.
Saving Data
Exporting to Excel
To save the collected data to an Excel file, use the export_to_excel method:
gtus.export_to_excel("google_trends_data.xlsx")
Exporting to JSON
To save the collected data to a JSON file, use the export_to_json method:
gtus.export_to_json("google_trends_data.json")
Accessing Data in Python
The data collected can be accessed in a nested dictionary format. After exporting to JSON, you can load the data and access it as follows:
import json
#Load the JSON data
with open("google_trends_data.json") as f:
data = json.load(f)
#Accessing data
print(data["CA"]["telemedicine"]) # Get telemedicine data for California
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.1.tar.gz.
File metadata
- Download URL: gtus-0.1.1.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d98cbe11ed9278a4a85c4d60234c3f8fddd3b036ac4fe6b78ecd45f4875e0835
|
|
| MD5 |
7578b62f1327972186b8c234b64c978e
|
|
| BLAKE2b-256 |
f70f3c66d5d5cea0b5dad251b78c6129abdac8e09688c92cfad05db47ca56c67
|
File details
Details for the file gtus-0.1.1-py3-none-any.whl.
File metadata
- Download URL: gtus-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.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 |
42229bc00093c94089bcadb61d3bda72187823cba45cf35c8acdf135ee0bf91a
|
|
| MD5 |
24f4f55f1d45a88dbdc999a7f8cd2579
|
|
| BLAKE2b-256 |
d77d334a232cc1af8ff71f8720bc27688f32dd0293c446099a313f1b80b90405
|