dballinari/GoogleTrends-Scraper
Tentang
Python implementation for scraping daily GoogleTrends data over long time periods
Data dan analisisPertumbuhan bintang terukur
- dalam 24 jam
- 0
- dalam 7 hari
- 0
- dalam 15 hari
- 0
- dalam 1 bulan
- Mengumpulkan riwayat
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
Asligtrends-scraper
Scrape daily Google Trends data over long time periods.
Google Trends only shows daily values for ranges of a few months. Longer ranges come back weekly or
monthly. gtrends-scraper downloads the range in short daily pieces, puts them on a common scale and
returns one daily series as a pandas.DataFrame. It drives a real (headless) browser through
Playwright, clicks the chart's CSV export button and reads the file.
Installation
pip install gtrends-scraper
playwright install chromium # one-time download of the browser
Python 3.10 or newer is required. If you would rather use a Google Chrome or Microsoft Edge that is
already installed, skip playwright install and pass channel="chrome" (or "msedge") to the scraper.
Usage
from gtrends_scraper import GoogleTrendsScraper
with GoogleTrendsScraper(sleep=2) as scraper:
data = scraper.get_trends("foo", "2018-01-01", "2019-03-31", region="US")
print(data.head())
# foo
# date
# 2018-01-01 52.13...
get_trends returns a DataFrame indexed by date with one column per keyword. Values are scaled so
that the largest value across all keywords and days equals 100, as on Google Trends.
| Argument | Meaning |
|---|---|
keywords |
a string or a list of strings |
start |
first day: a "YYYY-MM-DD" string, a date or a datetime |
end |
last day, inclusive |
region |
geo code such as "US" or "CH-ZH"; None (default) means worldwide |
category |
numeric Google Trends category, e.g. 7 for Finance |
More than five keywords are allowed. They are split into groups of five that all contain the first keyword, which is used to put the groups on the same scale, so put a keyword with steady search interest first.
Useful options of GoogleTrendsScraper:
| Option | Default | Meaning |
|---|---|---|
sleep |
1.0 |
base pause between requests; the real pause is sleep..2*sleep |
headless |
True |
set to False to watch the browser |
max_retries |
5 |
retries per request, with exponential back-off |
max_days |
200 |
longest range downloaded at daily frequency in one request |
date_format |
"%Y-%m-%d" |
format of date strings |
channel / executable_path |
None |
use an installed Chrome/Edge or a specific browser binary |
For a single query there is also a one-liner that opens and closes the browser for you:
from gtrends_scraper import get_trends
data = get_trends(["bitcoin", "ethereum"], "2020-01-01", "2021-12-31", sleep=2)
Command line
gtrends-scraper bitcoin ethereum --start 2020-01-01 --end 2021-12-31 --region US -o trends.csv
gtrends-scraper --help
Errors and rate limits
Google throttles automated traffic. When a download fails, the scraper waits and retries with
exponential back-off. If it still fails it raises gtrends_scraper.ScrapingError
(RateLimitError when Google answered "too many requests"). Larger sleep values help, and after
a block Google sometimes needs a pause of 30 minutes or more. Queries with too little search volume
return an empty or partly empty DataFrame. Logging (logging.getLogger("gtrends_scraper")) shows
what the scraper is doing. Set the environment variable GTRENDS_DEBUG_DIR to a folder to save a
screenshot and the HTML of every page on which the download failed.
How the rescaling works
Google Trends normalises every query so that its maximum is 100, so the daily pieces of a long range are each on a different scale. The scraper therefore
- downloads the whole range once at the frequency Google chooses (weekly or monthly),
- downloads the range in pieces of at most
max_daysdays at daily frequency, - sums each daily piece to weeks (or months) and computes the ratio to the weekly (monthly) values, and multiplies the piece by the median of those ratios.
In theory the ratio is constant within a piece. Google estimates search volumes from a random sample, so in practice it is not, which is why the median is used. Finally, the series is rescaled so that its maximum is 100.
Development
pip install -e ".[dev]"
playwright install chromium
pytest # unit tests and a browser test against a local mock page
ruff check . && mypy
GTRENDS_LIVE=1 pytest -m live # optional smoke test against the real Google Trends
Disclaimer
This project is not affiliated with Google. It automates the public Google Trends web page, which can change at any time and is subject to Google's terms of service. Use it responsibly and at a moderate request rate.
License
Jelajahi proyek terkait
Memuat…