-
-
Save ReinierKoops/448f510c9907845160bddc641696a3c9 to your computer and use it in GitHub Desktop.
Download CSV from Google Trends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python -m pip install playwright pandas | |
playwright install chromium |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from playwright.sync_api import sync_playwright | |
import pandas as pd | |
import datetime as dt | |
import pathlib | |
def download_trends_csv(geo: str, lang: str = "en-US"): | |
""" | |
Launch a headless browser, click Export ▸ Download CSV, | |
return a pandas DataFrame with the downloaded data. | |
""" | |
url = f"https://trends.google.com/trending?geo={geo}&hl={lang}" | |
outfile = pathlib.Path(f"trending_{geo}_{dt.date.today()}.csv") | |
with sync_playwright() as p: | |
browser = p.chromium.launch() | |
ctx = browser.new_context( | |
locale=lang, | |
accept_downloads=True, | |
user_agent=("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " | |
"AppleWebKit/537.36 (KHTML, like Gecko) " | |
"Chrome/124.0 Safari/537.36") | |
) | |
page = ctx.new_page() | |
page.goto(url, timeout=60_000) | |
page.wait_for_load_state("networkidle") | |
# -- dismiss cookie / consent banner if present -- | |
consent_texts = ( | |
"I agree", "Accept all", # English variants | |
) | |
for txt in consent_texts: | |
loc = page.get_by_role("button", name=txt) | |
if loc.is_visible(): | |
loc.click() | |
break | |
# -- click Export ▸ Download CSV -- | |
export_btn = page.get_by_role("button", name="Export") | |
export_btn.wait_for(state="visible", timeout=20_000) | |
with page.expect_download() as download_info: | |
export_btn.click() | |
menuitem = page.get_by_role("menuitem", name="Download CSV") | |
menuitem.wait_for(state="visible") | |
menuitem.click() | |
download = download_info.value | |
download.save_as(outfile) | |
browser.close() | |
df = pd.read_csv(outfile) | |
print(f"\n✅ Saved {len(df)} rows → {outfile}\n") | |
return df | |
# ---------------------------------------------------------------------- | |
if __name__ == "__main__": | |
df = download_trends_csv() | |
print(df.head()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment