Skip to content

Commit 3b3c341

Browse files
committed
add extracing google trends data tutorial
1 parent c2bf474 commit 3b3c341

File tree

5 files changed

+329
-0
lines changed

5 files changed

+329
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
116116
- [Automated Browser Testing with Edge and Selenium in Python](https://www.thepythoncode.com/article/automated-browser-testing-with-edge-and-selenium-in-python). ([code](web-scraping/selenium-edge-browser))
117117
- [How to Automate Login using Selenium in Python](https://www.thepythoncode.com/article/automate-login-to-websites-using-selenium-in-python). ([code](web-scraping/automate-login))
118118
- [How to Make a Currency Converter in Python](https://www.thepythoncode.com/article/make-a-currency-converter-in-python). ([code](web-scraping/currency-converter))
119+
- [[How to Extract Google Trends Data in Python](https://www.thepythoncode.com/article/extract-google-trends-data-in-python). ([code](web-scraping/extract-google-trends-data))]
119120

120121
- ### [Python Standard Library](https://www.thepythoncode.com/topic/python-standard-library)
121122
- [How to Transfer Files in the Network using Sockets in Python](https://www.thepythoncode.com/article/send-receive-files-using-sockets-python). ([code](general/transfer-files/))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"colab": {
8+
"base_uri": "https://localhost:8080/"
9+
},
10+
"id": "Mzt6JmpwLnl0",
11+
"outputId": "bb600d41-0b2f-4c7e-ab47-0e645277988b"
12+
},
13+
"outputs": [],
14+
"source": [
15+
"!pip install pytrends"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"metadata": {
22+
"id": "9S9Szn46Lp65"
23+
},
24+
"outputs": [],
25+
"source": [
26+
"from pytrends.request import TrendReq\n",
27+
"import seaborn\n",
28+
"# for styling\n",
29+
"seaborn.set_style(\"darkgrid\")\n",
30+
"\n",
31+
"# initialize a new Google Trends Request Object\n",
32+
"pt = TrendReq(hl=\"en-US\", tz=360)"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {
39+
"id": "_3hktMxsLq4M"
40+
},
41+
"outputs": [],
42+
"source": [
43+
"# set the keyword & timeframe\n",
44+
"pt.build_payload([\"Python\", \"Java\"], timeframe=\"all\")"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {
51+
"colab": {
52+
"base_uri": "https://localhost:8080/",
53+
"height": 455
54+
},
55+
"id": "5bj7PEPZLruO",
56+
"outputId": "94d313cb-c106-43e8-921f-edc7b7a82ec3"
57+
},
58+
"outputs": [],
59+
"source": [
60+
"# get the interest over time\n",
61+
"iot = pt.interest_over_time()\n",
62+
"iot"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"metadata": {
69+
"colab": {
70+
"base_uri": "https://localhost:8080/",
71+
"height": 406
72+
},
73+
"id": "BG8uQd3zLsw-",
74+
"outputId": "1f9d7327-bad5-4d05-8b1d-046c7527864c"
75+
},
76+
"outputs": [],
77+
"source": [
78+
"# plot it\n",
79+
"iot.plot(figsize=(10, 6))"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {
86+
"colab": {
87+
"base_uri": "https://localhost:8080/",
88+
"height": 455
89+
},
90+
"id": "65qkNWguL8g-",
91+
"outputId": "43332253-4215-48f7-e25a-c4dcc2a9f99a"
92+
},
93+
"outputs": [],
94+
"source": [
95+
"# get hourly historical interest\n",
96+
"data = pt.get_historical_interest(\n",
97+
" [\"data science\"], \n",
98+
" cat=396, \n",
99+
" year_start=2022, month_start=1, day_start=1, hour_start=0,\n",
100+
" year_end=2022, month_end=2, day_end=10, hour_end=23,\n",
101+
")\n",
102+
"data"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": null,
108+
"metadata": {
109+
"id": "NYoPNiSiVWFo"
110+
},
111+
"outputs": [],
112+
"source": [
113+
"# the keyword to extract data\n",
114+
"kw = \"python\"\n",
115+
"pt.build_payload([kw], timeframe=\"all\")\n",
116+
"# get the interest by country\n",
117+
"ibr = pt.interest_by_region(\"COUNTRY\", inc_low_vol=True, inc_geo_code=True)"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {
124+
"colab": {
125+
"base_uri": "https://localhost:8080/"
126+
},
127+
"id": "bjWBRVS-XNfo",
128+
"outputId": "db776b26-2487-4ba1-d096-9c84dec93d1f"
129+
},
130+
"outputs": [],
131+
"source": [
132+
"# sort the countries by interest\n",
133+
"ibr[kw].sort_values(ascending=False)"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {
140+
"colab": {
141+
"base_uri": "https://localhost:8080/",
142+
"height": 833
143+
},
144+
"id": "ny8R1DKBXON4",
145+
"outputId": "8abbf1af-a186-42ea-91a2-56c5bd165f7d"
146+
},
147+
"outputs": [],
148+
"source": [
149+
"# get related topics of the keyword\n",
150+
"rt = pt.related_topics()\n",
151+
"rt[kw][\"top\"]"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {
158+
"colab": {
159+
"base_uri": "https://localhost:8080/",
160+
"height": 833
161+
},
162+
"id": "dCXqM2uBXv7M",
163+
"outputId": "686c8144-50f5-445c-929f-dfda1494a38b"
164+
},
165+
"outputs": [],
166+
"source": [
167+
"# get related queries to previous keyword\n",
168+
"rq = pt.related_queries()\n",
169+
"rq[kw][\"top\"]"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {
176+
"colab": {
177+
"base_uri": "https://localhost:8080/"
178+
},
179+
"id": "FYCQ-ejxZv40",
180+
"outputId": "2ee09da5-6d19-4ba1-c44a-5dd15a683387"
181+
},
182+
"outputs": [],
183+
"source": [
184+
"# get suggested searches\n",
185+
"pt.suggestions(\"python\")"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"metadata": {
192+
"colab": {
193+
"base_uri": "https://localhost:8080/"
194+
},
195+
"id": "jFvUTnBBaYTS",
196+
"outputId": "95ca6d7d-f801-467d-ade1-e2d5c009bcf5"
197+
},
198+
"outputs": [],
199+
"source": [
200+
"# another example of suggested searches\n",
201+
"pt.suggestions(\"America\")"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": null,
207+
"metadata": {
208+
"colab": {
209+
"base_uri": "https://localhost:8080/",
210+
"height": 676
211+
},
212+
"id": "AEKb0IY0YLSx",
213+
"outputId": "8c4519ba-143b-48e2-fc87-b5c5d9a7d56f"
214+
},
215+
"outputs": [],
216+
"source": [
217+
"# trending searches per region\n",
218+
"ts = pt.trending_searches(pn=\"united_kingdom\")\n",
219+
"ts"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {
226+
"colab": {
227+
"base_uri": "https://localhost:8080/",
228+
"height": 423
229+
},
230+
"id": "vFDybnL-YaiF",
231+
"outputId": "c826398b-6a1f-4bc2-f5b8-1c1b8ea8f0f8"
232+
},
233+
"outputs": [],
234+
"source": [
235+
"# real-time trending searches\n",
236+
"pt.realtime_trending_searches()"
237+
]
238+
}
239+
],
240+
"metadata": {
241+
"colab": {
242+
"collapsed_sections": [],
243+
"name": "Extracting-GoogleTrends-Data_PythonCodeTutorial.ipynb",
244+
"provenance": []
245+
},
246+
"kernelspec": {
247+
"display_name": "Python 3",
248+
"name": "python3"
249+
},
250+
"language_info": {
251+
"name": "python"
252+
}
253+
},
254+
"nbformat": 4,
255+
"nbformat_minor": 0
256+
}

Diff for: web-scraping/extract-google-trends-data/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Extract Google Trends Data in Python](https://www.thepythoncode.com/article/extract-google-trends-data-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
"""Extracting-GoogleTrends-Data_PythonCodeTutorial.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1lMX3VemgcfGpiGNlQJNPyivSXHAVYe6O
8+
"""
9+
10+
# !pip install pytrends
11+
12+
from pytrends.request import TrendReq
13+
import seaborn
14+
# for styling
15+
seaborn.set_style("darkgrid")
16+
17+
# initialize a new Google Trends Request Object
18+
pt = TrendReq(hl="en-US", tz=360)
19+
20+
# set the keyword & timeframe
21+
pt.build_payload(["Python", "Java"], timeframe="all")
22+
23+
# get the interest over time
24+
iot = pt.interest_over_time()
25+
iot
26+
27+
# plot it
28+
iot.plot(figsize=(10, 6))
29+
30+
# get hourly historical interest
31+
data = pt.get_historical_interest(
32+
["data science"],
33+
cat=396,
34+
year_start=2022, month_start=1, day_start=1, hour_start=0,
35+
year_end=2022, month_end=2, day_end=10, hour_end=23,
36+
)
37+
data
38+
39+
# the keyword to extract data
40+
kw = "python"
41+
pt.build_payload([kw], timeframe="all")
42+
# get the interest by country
43+
ibr = pt.interest_by_region("COUNTRY", inc_low_vol=True, inc_geo_code=True)
44+
45+
# sort the countries by interest
46+
ibr[kw].sort_values(ascending=False)
47+
48+
# get related topics of the keyword
49+
rt = pt.related_topics()
50+
rt[kw]["top"]
51+
52+
# get related queries to previous keyword
53+
rq = pt.related_queries()
54+
rq[kw]["top"]
55+
56+
# get suggested searches
57+
pt.suggestions("python")
58+
59+
# another example of suggested searches
60+
pt.suggestions("America")
61+
62+
# trending searches per region
63+
ts = pt.trending_searches(pn="united_kingdom")
64+
ts
65+
66+
# real-time trending searches
67+
pt.realtime_trending_searches()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytrends
2+
seaborn

0 commit comments

Comments
 (0)