[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-72453":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":16,"stars30d":17,"stars90d":16,"forks30d":16,"starsTrendScore":16,"compositeScore":18,"rankGlobal":10,"rankLanguage":10,"license":10,"archived":19,"fork":19,"defaultBranch":20,"hasWiki":19,"hasPages":19,"topics":21,"createdAt":10,"pushedAt":10,"updatedAt":36,"readmeContent":37,"aiSummary":38,"trendingCount":16,"starSnapshotCount":16,"syncStatus":39,"lastSyncTime":40,"discoverSource":41},72453,"how-to-scrape-google-trends","oxylabs\u002Fhow-to-scrape-google-trends","oxylabs","Learn step-by-step how to scrape Google Trends data and make a result comparison using Python and Oxylabs SERP API. Extract keywords, their popularity, breakdown by region, related queries, and more.","https:\u002F\u002Foxylabs.io\u002Fproducts\u002Fscraper-api\u002Fserp",null,"Python",2580,17,8,1,0,64,25.77,false,"main",[22,23,24,25,26,27,28,29,30,31,32,33,34,35],"google","google-trend","google-trend-api","google-trend-strategy","google-trends","google-trends-api","google-trends-crawler","google-trends-scraper","python","scraper","scraper-api","scraping","scraping-api","web-scraping","2026-06-12 02:03:03","# How to Scrape Google Trends Data With Python\n\n[![Oxylabs promo code](https:\u002F\u002Fraw.githubusercontent.com\u002Foxylabs\u002Fhow-to-scrape-google-scholar\u002Frefs\u002Fheads\u002Fmain\u002FGoogle-Scraper-API-1090x275.png)](https:\u002F\u002Foxylabs.io\u002Fproducts\u002Fscraper-api\u002Fserp\u002Fgoogle?utm_source=877&utm_medium=affiliate&groupid=877&utm_content=how-to-scrape-google-trends-github&transaction_id=102c8d36f7f0d0e5797b8f26152160)\n\n[![](https:\u002F\u002Fdcbadge.limes.pink\u002Fapi\u002Fserver\u002FPds3gBmKMH?style=for-the-badge&theme=discord)](https:\u002F\u002Fdiscord.gg\u002FPds3gBmKMH) [![YouTube](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FYouTube-Oxylabs-red?style=for-the-badge&logo=youtube&logoColor=white)](https:\u002F\u002Fwww.youtube.com\u002F@oxylabs)\n\n- [Why scrape Google Trends data?](#why-scrape-google-trends-data)\n- [1. Install libraries](#1-install-libraries)\n- [2. Send a request](#2-send-a-request)\n- [3. Save results to CSV](#3-save-results-to-csv)\n- [4. Create a result comparison](#4-create-a-result-comparison)\n\nThis article provides step-by-step instructions on how to get Google Trends data with Python and [SERP Scraper API](https:\u002F\u002Fdevelopers.oxylabs.io\u002Fscraper-apis\u002Fweb-scraper-api) (a part of Web Scraper API), which requires a **paid subscription** or a **free trial**.\n\n\n\n## Why scrape Google Trends data?\nHere are some of the uses for scraped Google Trends data:\n\n- **Keyword research:** Google Trends is widely used among SEO specialists and content marketers. Since it provides insights into the past and present popularity of search terms, these professionals can tailor their marketing strategies to gain more website traffic.\n\n- **Market research:** Google Trends data can be used for market research, helping businesses understand consumer interests and preferences over time. For example, e-commerce businesses can use Google Trends search insights for product development.\n\n- **Societal research:** Google Trends website is a valuable resource for journalists and researchers, offering a glimpse into societal trends and public interest in specific topics.\n\nThese are just a few examples. Google Trends data can also help with investment decisions, brand reputation monitoring, and other cases.\n\n## 1. Install libraries\n\nFor this guide, you'll need the following:\n- Credentials for [SERP Scraper API](https:\u002F\u002Foxylabs.io\u002Fproducts\u002Fscraper-api\u002Fserp) – you can claim a **7-day free trial** by registering on the [dashboard](https:\u002F\u002Fdashboard.oxylabs.io\u002Fen\u002F);\n- [Python](https:\u002F\u002Fwww.python.org\u002Fdownloads\u002F);\n- [Requests](https:\u002F\u002Frequests.readthedocs.io\u002Fen\u002Flatest\u002F) library to make requests;\n- [Pandas](https:\u002F\u002Fpandas.pydata.org\u002Fdocs\u002Findex.html) library to manipulate received data.\n\nOpen your terminal and run the following `pip` command:\n```bash\npip install requests pandas\n```\n\nThen, import these libraries in a new Python file:\n\n```python\nimport requests\nimport pandas as pd\n```\n\n## 2. Send a request\n\nLet’s begin with building an initial request to the API:\n\n```python\nimport requests\nfrom pprint import pprint\n\nUSERNAME = \"YourUsername\"\nPASSWORD = \"YourPassword\"\n\nquery = \"persian cat\"\n\nprint(f\"Getting data from Google Trends for {query} keyword..\")\n\nurl = \"https:\u002F\u002Frealtime.oxylabs.io\u002Fv1\u002Fqueries\"\nauth = (USERNAME, PASSWORD)\n\npayload = {\n       \"source\": \"google_trends_explore\",\n       \"query\": query,\n}\n\ntry:\n    response = requests.request(\"POST\", url, auth=auth, json=payload, timeout=180)\nexcept requests.exceptions.RequestException as e:\n    print(\"Caught exception while getting trend data\")\n    raise e\n\ndata = response.json()\ncontent = data[\"results\"][0][\"content\"]\npprint(content)\n```\n\nFor more information about possible parameters, check our [documentation](https:\u002F\u002Fdevelopers.oxylabs.io\u002Fscraper-apis\u002Fweb-scraper-api\u002Fgoogle\u002Ftrends-explore).\n\nIf everything’s in order, when you run the code, you should see the raw results of the query in the terminal window like this:\n![](images\u002Ftrends_data.png)\n\n## 3. Save results to CSV\n\nNow that you have the results, adjust the formatting and save in the CSV format – this way, it’ll be easier to analyze the data. All this can be done with the help of the `pandas` Python library.\n\nThe response you get from the API provides you with four categories of information: `interest_over_time`, `breakdown_by_region`, `related_topics`, and `related_queries`. Let’s split each category into its own separate CSV file. \n\nBegin by converting each into a `pandas` dataframe:\n```python\ndef flatten_topic_data(topics_data: List[dict]) -> List[dict]:\n   \"\"\"Flattens related_topic data\"\"\"\n   topics_items = []\n   for item in topics_data[0][\"items\"]:\n       item_dict = {\n           \"mid\": item[\"topic\"][\"mid\"],\n           \"title\": item[\"topic\"][\"title\"],\n           \"type\": item[\"topic\"][\"type\"],\n           \"value\": item[\"value\"],\n           \"formatted_value\": item[\"formatted_value\"],\n           \"link\": item[\"link\"],\n           \"keyword\": topics_data[0][\"keyword\"],\n       }\n       topics_items.append(item_dict)\n\n   return topics_items\n\ntrend_data = json.loads(content)\nprint(\"Creating dataframes..\")\n\n   # Interest over time\niot_df = pd.DataFrame(trend_data[\"interest_over_time\"][0][\"items\"])\niot_df[\"keyword\"] = trend_data[\"interest_over_time\"][0][\"keyword\"]\n\n   # Breakdown by region\nbbr_df = pd.DataFrame(trend_data[\"breakdown_by_region\"][0][\"items\"])\nbbr_df[\"keyword\"] = trend_data[\"breakdown_by_region\"][0][\"keyword\"]\n\n   # Related topics\nrt_data = flatten_topic_data(trend_data[\"related_topics\"])\nrt_df = pd.DataFrame(rt_data)\n\n   # Related queries\nrq_df = pd.DataFrame(trend_data[\"related_queries\"][0][\"items\"])\nrq_df[\"keyword\"] = trend_data[\"related_queries\"][0][\"keyword\"]\n```\n\nAs the data for `related_topics` is multi-leveled, you'll have to flatten the structure into a single-leveled one. Thus, the function `flatten_topic_data` was added to do so. \n\nThe only thing left is to save the data to a file:\n```python\nCSV_FILE_DIR = \".\u002Fcsv\u002F\"\n\nkeyword = trend_data[\"interest_over_time\"][0][\"keyword\"]\n   keyword_path = os.path.join(CSV_FILE_DIR, keyword)\n   try:\n       os.makedirs(keyword_path, exist_ok=True)\n   except OSError as e:\n       print(\"Caught exception while creating directories\")\n       raise e\n\n   print(\"Dumping to csv..\")\n   iot_df.to_csv(f\"{keyword_path}\u002Finterest_over_time.csv\", index=False)\n   bbr_df.to_csv(f\"{keyword_path}\u002Fbreakdown_by_region.csv\", index=False)\n   rt_df.to_csv(f\"{keyword_path}\u002Frelated_topics.csv\", index=False)\n   rq_df.to_csv(f\"{keyword_path}\u002Frelated_queries.csv\", index=False)\n```\nYou’ve now created a folder structure to hold all of your separate CSV files grouped by keyword:\n\n![](images\u002Ftrends_data_csv.png)\n\n## 4. Create a result comparison\nLet’s begin with multiple keyword handling. To make the code iterable, split it into reusable functions.\n\nFirst, extract the code for the request to the API into a function that takes a query as an argument and returns you the response:\n\n```python\ndef get_trend_data(query: str) -> dict:\n   \"\"\"Gets a dictionary of trends based on given query string from Google Trends via SERP Scraper API\"\"\"\n   print(f\"Getting data from Google Trends for {query} keyword..\")\n   url = \"https:\u002F\u002Frealtime.oxylabs.io\u002Fv1\u002Fqueries\"\n   auth = (USERNAME, PASSWORD)\n   payload = {\n       \"source\": \"google_trends_explore\",\n       \"query\": query,\n   }\n   try:\n       response = requests.request(\"POST\", url, auth=auth, json=payload)\n   except requests.exceptions.RequestException as e:\n       print(\"Caught exception while getting trend data\")\n       raise e\n\n   data = response.json()\n   content = data[\"results\"][0][\"content\"]\n   return json.loads(content)\n```\nNext, you need a function that would transform a raw response into `pandas` dataframes, save said dataframes as CSV files, and return them:\n```python\ndef dump_trend_data_to_csv(trend_data: dict) -> dict:\n   \"\"\"Dumps given trend data to generated CSV file\"\"\"\n   CSV_FILE_DIR = \".\u002Fcsv\u002F\"\n   # Interest over time\n   print(\"Creating dataframes..\")\n   iot_df = pd.DataFrame(trend_data[\"interest_over_time\"][0][\"items\"])\n   iot_df[\"keyword\"] = trend_data[\"interest_over_time\"][0][\"keyword\"]\n\n   # Breakdown by region\n   bbr_df = pd.DataFrame(trend_data[\"breakdown_by_region\"][0][\"items\"])\n   bbr_df[\"keyword\"] = trend_data[\"breakdown_by_region\"][0][\"keyword\"]\n\n   # Related topics\n   rt_data = flatten_topic_data(trend_data[\"related_topics\"])\n   rt_df = pd.DataFrame(rt_data)\n\n   # Related queries\n   rq_df = pd.DataFrame(trend_data[\"related_queries\"][0][\"items\"])\n   rq_df[\"keyword\"] = trend_data[\"related_queries\"][0][\"keyword\"]\n\n   keyword = trend_data[\"interest_over_time\"][0][\"keyword\"]\n   keyword_path = os.path.join(CSV_FILE_DIR, keyword)\n   try:\n       os.makedirs(keyword_path, exist_ok=True)\n   except OSError as e:\n       print(\"Caught exception while creating directories\")\n       raise e\n\n   print(\"Dumping to csv..\")\n   iot_df.to_csv(f\"{keyword_path}\u002Finterest_over_time.csv\", index=False)\n   bbr_df.to_csv(f\"{keyword_path}\u002Fbreakdown_by_region.csv\", index=False)\n   rt_df.to_csv(f\"{keyword_path}\u002Frelated_topics.csv\", index=False)\n   rq_df.to_csv(f\"{keyword_path}\u002Frelated_queries.csv\", index=False)\n\n   result_set = {}\n   result_set[\"iot\"] = iot_df\n   result_set[\"bbr\"] = bbr_df\n   result_set[\"rt\"] = rt_df\n   result_set[\"rq\"] = rq_df\n\n   return result_set\n```\nNow that the request and dataframe creation is covered, you can create comparisons:\n\n```python\ndef create_comparison(trend_dataframes : dict) -> None:\n   comparison = trend_dataframes[0]\n   i = 1\n\n   for df in trend_dataframes[1:]:\n       comparison[\"iot\"] = pd.merge(comparison[\"iot\"], df[\"iot\"], on=\"time\", suffixes=(\"\", f\"_{i}\"))\n       comparison[\"bbr\"] = pd.merge(comparison[\"bbr\"], df[\"bbr\"], on=\"geo_code\", suffixes=(\"\", f\"_{i}\"))\n       comparison[\"rt\"] = pd.merge(comparison[\"rt\"], df[\"rt\"], on=\"title\", how=\"inner\", suffixes=(\"\", f\"_{i}\"))\n       comparison[\"rq\"] = pd.merge(comparison[\"rq\"], df[\"rq\"], on=\"query\", how=\"inner\", suffixes=(\"\", f\"_{i}\"))\n       i = i + 1\n\n   comparison[\"iot\"].to_csv(\"comparison_interest_over_time.csv\", index=False)\n   comparison[\"bbr\"].to_csv(\"comparison_breakdown_by_region.csv\", index=False)\n   comparison[\"rt\"].to_csv(\"comparison_related_topics.csv\", index=False)\n   comparison[\"rq\"].to_csv(\"comparison_related_queries.csv\", index=False)\n```\nThis function will accept the dataframes for all the queries you have created, go over them, and merge them for comparison on key metrics.\n\nThe last thing to do is to create the core logic of your application. Adding it all together, the final version of the code should look like this: \n\n```python\nimport json\nimport os\nfrom typing import List\n\nimport pandas as pd\nimport requests\n\ndef get_trend_data(query: str) -> dict:\n   \"\"\"Gets a dictionary of trends based on given query string from Google Trends via SERP Scraper API\"\"\"\n\n   USERNAME = \"yourUsername\"\n   PASSWORD = \"yourPassword\"\n   print(f\"Getting data from Google Trends for {query} keyword..\")\n   url = \"https:\u002F\u002Frealtime.oxylabs.io\u002Fv1\u002Fqueries\"\n   auth = (USERNAME, PASSWORD)\n   payload = {\n       \"source\": \"google_trends_explore\",\n       \"query\": query,\n   }\n   try:\n       response = requests.request(\"POST\", url, auth=auth, json=payload)\n   except requests.exceptions.RequestException as e:\n       print(\"Caught exception while getting trend data\")\n       raise e\n\n   data = response.json()\n   content = data[\"results\"][0][\"content\"]\n   return json.loads(content)\n\n\ndef flatten_topic_data(topics_data: List[dict]) -> List[dict]:\n   \"\"\"Flattens related_topic data\"\"\"\n   topics_items = []\n   for item in topics_data[0][\"items\"]:\n       item_dict = {\n           \"mid\": item[\"topic\"][\"mid\"],\n           \"title\": item[\"topic\"][\"title\"],\n           \"type\": item[\"topic\"][\"type\"],\n           \"value\": item[\"value\"],\n           \"formatted_value\": item[\"formatted_value\"],\n           \"link\": item[\"link\"],\n           \"keyword\": topics_data[0][\"keyword\"],\n       }\n       topics_items.append(item_dict)\n\n   return topics_items\n\n\ndef dump_trend_data_to_csv(trend_data: dict) -> dict:\n   \"\"\"Dumps given trend data to generated CSV file\"\"\"\n   CSV_FILE_DIR = \".\u002Fcsv\u002F\"\n   # Interest over time\n   print(\"Creating dataframes..\")\n   iot_df = pd.DataFrame(trend_data[\"interest_over_time\"][0][\"items\"])\n   iot_df[\"keyword\"] = trend_data[\"interest_over_time\"][0][\"keyword\"]\n\n   # Breakdown by region\n   bbr_df = pd.DataFrame(trend_data[\"breakdown_by_region\"][0][\"items\"])\n   bbr_df[\"keyword\"] = trend_data[\"breakdown_by_region\"][0][\"keyword\"]\n\n   # Related topics\n   rt_data = flatten_topic_data(trend_data[\"related_topics\"])\n   rt_df = pd.DataFrame(rt_data)\n\n   # Related queries\n   rq_df = pd.DataFrame(trend_data[\"related_queries\"][0][\"items\"])\n   rq_df[\"keyword\"] = trend_data[\"related_queries\"][0][\"keyword\"]\n\n   keyword = trend_data[\"interest_over_time\"][0][\"keyword\"]\n   keyword_path = os.path.join(CSV_FILE_DIR, keyword)\n   try:\n       os.makedirs(keyword_path, exist_ok=True)\n   except OSError as e:\n       print(\"Caught exception while creating directories\")\n       raise e\n\n   print(\"Dumping to csv..\")\n   iot_df.to_csv(f\"{keyword_path}\u002Finterest_over_time.csv\", index=False)\n   bbr_df.to_csv(f\"{keyword_path}\u002Fbreakdown_by_region.csv\", index=False)\n   rt_df.to_csv(f\"{keyword_path}\u002Frelated_topics.csv\", index=False)\n   rq_df.to_csv(f\"{keyword_path}\u002Frelated_queries.csv\", index=False)\n\n   result_set = {}\n   result_set[\"iot\"] = iot_df\n   result_set[\"bbr\"] = bbr_df\n   result_set[\"rt\"] = rt_df\n   result_set[\"rq\"] = rq_df\n\n   return result_set\n\ndef create_comparison(trend_dataframes : dict) -> None:\n   comparison = trend_dataframes[0]\n   i = 1\n\n   for df in trend_dataframes[1:]:\n       comparison[\"iot\"] = pd.merge(comparison[\"iot\"], df[\"iot\"], on=\"time\", suffixes=(\"\", f\"_{i}\"))\n       comparison[\"bbr\"] = pd.merge(comparison[\"bbr\"], df[\"bbr\"], on=\"geo_code\", suffixes=(\"\", f\"_{i}\"))\n       comparison[\"rt\"] = pd.merge(comparison[\"rt\"], df[\"rt\"], on=\"title\", how=\"inner\", suffixes=(\"\", f\"_{i}\"))\n       comparison[\"rq\"] = pd.merge(comparison[\"rq\"], df[\"rq\"], on=\"query\", how=\"inner\", suffixes=(\"\", f\"_{i}\"))\n       i = i + 1\n\n   comparison[\"iot\"].to_csv(\"comparison_interest_over_time.csv\", index=False)\n   comparison[\"bbr\"].to_csv(\"comparison_breakdown_by_region.csv\", index=False)\n   comparison[\"rt\"].to_csv(\"comparison_related_topics.csv\", index=False)\n   comparison[\"rq\"].to_csv(\"comparison_related_queries.csv\", index=False)\n\ndef main():\n   keywords = [\"cat\", \"cats\"]\n\n   results = []\n\n   for keyword in keywords:\n       trend_data = get_trend_data(keyword)\n       df_set = dump_trend_data_to_csv(trend_data)\n       results.append(df_set)\n\n   create_comparison(results)\n\nif __name__ == \"__main__\":\n   main()\n```\n\nRunning the code will create comparison CSV files that have the combined information of the supplied keywords on each of the categories:\n\n- `interest_over_time`\n\n- `breakdown_by_region`\n\n- `related_topics`\n\n- `related_queries`\n\n![](images\u002Ftrends_data_comparison.png)\n\nLooking to scrape data from other Google sources? [Google Sheets for Basic Web Scraping](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fweb-scraping-google-sheets), [Google Play Scraper](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fgoogle-play-scraper), [How To Scrape Google Jobs](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fhow-to-scrape-google-jobs), [Google News Scrpaer](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fgoogle-news-scraper), [How to Scrape Google Scholar](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fhow-to-scrape-google-scholar), [How to Scrape Google Flights with Python](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fhow-to-scrape-google-flights),  [Scrape Google Search Results](https:\u002F\u002Fgithub.com\u002Foxylabs\u002Fscrape-google-python)\n","该项目展示了如何使用Python和Oxylabs SERP API抓取Google Trends数据，并进行结果对比。其核心功能包括提取关键词、流行度、地区分布、相关查询等信息。通过结合Requests库发送请求及Pandas库处理数据，用户可以轻松获取并分析趋势数据。适用于SEO专家进行关键词研究、企业开展市场调研以了解消费者兴趣变化、以及研究人员探索社会趋势等多个场景。",2,"2026-06-11 03:42:06","high_star"]