Python 3 Google Autocomplete & Suggest API Example to Generate Long Tail Keywords and Save as CSV
87 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-google-autocomplete-suggest-api-example-to-generate-long-tail-keywords-and-save-as-csv/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:03
in this video i will show you a python
0:05
script which will
0:07
actually build out a keyword explorer
0:11
where you can actually get keyword ideas
0:13
related to google search and for this
0:16
purpose we will be using the google
0:19
autocomplete and suggestions api google
0:22
autocomplete and suggest api i just
0:25
written this python script right here
0:27
which actually make use of that google
0:29
api to get long long-term longtail
0:33
keywords so that you can rank your
0:35
youtube videos or blogs so this is
0:37
actually a python script and uh if i
0:40
just run this python script so just see
0:43
as i run this python script here you
0:46
just need to specify a seed keyword so
0:50
as i run this python script the user
0:53
will be asked to input a keyword so you
0:58
can enter any keyword here and then it
1:00
will give you the suggestions and
1:02
autocomplete so we simply write here
1:05
python script 2 so we are just writing
1:08
this seed keyword the basic keyword
1:11
right here now it will autocomplete
1:12
everything so as i click enter you will
1:16
see it will fetch all the suggestions
1:19
coming from the autocomplete and suggest
1:21
api and it will store all those keywords
1:25
longtail keywords inside a csv file so
1:28
just wait it actually takes 5 to 10
1:31
seconds and then you can see it has
1:33
saved 245 longtail keywords inside the
1:37
csv file and if i try to open this file
1:40
you will see that these are the keywords
1:43
right here you can
1:45
see so you can see it has made a
1:49
complete list where it actually
1:52
autocomplete and it has extracted all
1:54
these longtail keywords which will help
1:56
you in ranking your website or making
1:59
youtube videos so it's a very awesome
2:02
application what it is actually doing
2:04
behind the scenes whenever you go to
2:07
google.com as you type the keyword these
2:11
autocomplete suggestions which come
2:13
right here you will see these are called
2:15
autocomplete suggestions so we are
2:18
programmatically fetching all these
2:21
suggestions by a python script and
2:23
saving it inside a csv file for this we
2:26
are using this google autocomplete
2:30
api and also we are using the google
2:33
suggest api as
2:35
well so which actually makes use
2:39
of to give you suggestions so for this
2:44
we don't need to install any keyword
2:46
right here so the full script is given
2:50
in the description of this video so now
2:52
let me show you how to build this script
2:56
from scratch so just make a simple app
2:59
py file and first of all we need to
3:03
import the required modules the request
3:06
module we need to import then we need to
3:09
also import the csv module and then we
3:12
also need to import the time module and
3:15
then we specify or we
3:19
actually take the user input and we take
3:22
the input of the user here we actually
3:25
allow the user to enter a keyword base
3:29
keyword as the user write the keyword
3:32
but we will actually call this function
3:34
which is generate long tail
3:39
keyword so we define this function and
3:42
inside this function we are actually
3:44
calling
3:45
this user input so we are passing this
3:48
to this function so now we just need to
3:51
define this function so this will be
3:55
generate
3:59
longtail
4:00
keywords and here we specify our user
4:04
input which is passed as an argument so
4:06
now in this
4:07
function you just need to actually make
4:11
a
4:14
uh variable here right here which will
4:16
be long tail keywords which will be an
4:19
empty array so we declare this set array
4:23
and then we extract all the seed phrases
4:26
or the autocomplete suggestions using
4:29
the base
4:31
keyword like
4:33
this and then we simply concatenate this
4:37
by space and then
4:40
chr and
4:43
then we loop through for i in range and
4:49
we basically loop through this is
4:51
actually as sky code here of each
4:53
character 97 to 123 so this is
4:57
essentially going from a to z
5:00
alphabetically and uh after
5:03
that we loop through each phrase or for
5:07
phrase and seed
5:09
phrases and then now to actually inside
5:12
this loop we need
5:16
to get the suggestions for getting the
5:19
suggestions we will define another
5:21
function right here get
5:23
suggestions using the
5:29
phrase so we can print out this seed
5:32
phrase so that you can actually see what
5:34
is
5:36
happening so now we just need to define
5:39
this function get suggestions which will
5:41
actually extract all the suggestions for
5:46
us and inside this function we are
5:50
passing the phrase or the keyword you
5:53
may
5:56
say so inside this we need to hit a url
6:00
so this url is autocomplete and suggest
6:03
api so this is provided by google it's
6:06
completely free you can use it unlimited
6:09
number of time so this is actually the
6:12
endpoint we are making a simple get
6:14
request https suggest
6:19
queries.google.com/complete/ search so
6:21
here we just need to make a simple get
6:23
request and just here we specify the
6:26
parameters which is your browser client
6:29
it can be chrome or firefox but you just
6:32
need to provide here firefox which works
6:35
best and then the second parameter which
6:38
is your
6:41
keyword then we simply make the request
6:45
to get the
6:49
response so here we specify use the
6:52
request module the get
6:55
method to the url and alongside with the
6:59
parameters uh after that if the response
7:01
comes we can simply print out the
7:04
response and if the response comes here
7:08
we will check inside this if condition
7:11
that if the
7:13
response status code is equal to 200 in
7:17
that case we need
7:18
to actually return this
7:24
response response dot
7:28
json like
7:31
and if any sort of exception take place
7:33
then we just need to print out the
7:35
exception and return this empty
7:38
array and if the response is not 200
7:42
then also we need to return empty array
7:44
so this is actually a very simple
7:46
function we specify the keyword then it
7:49
actually make this get request to this
7:51
api google suggest autocomplete api and
7:55
we get all the suggestions and then we
7:57
return those suggestions from this
8:00
function that's all
8:03
so at this moment of time if i want to
8:07
execute
8:09
this so it is saying that uh invalid
8:13
syntax on line number 28
8:18
uh just wait i think i made a mistake
8:21
right here which is
8:27
uh just wait the full script is given
8:30
guys in the description so no no need to
8:32
worry about
8:34
it uh i think on this line sorry let me
8:38
just see paste
8:42
it
8:44
so again if i run
8:48
this here you need to specify the base
8:51
keyword so python script
8:54
2 so it is saying that base keyword is
8:57
not defined on line
9:00
number
9:03
28 oh sorry this is user input sorry
9:06
just need to call this space
9:18
keyword so as you specify your keyword
9:20
you will
9:21
see it will generate this response right
9:24
here it you can see that all this 200
9:27
response coming python script 2 a and
9:30
then b c d e f gh so alphabetically we
9:34
are getting all these suggestions now we
9:36
just need to display these suggestions
9:38
and store it in a csv file so after we
9:41
get these suggestions right in this
9:43
variable
9:45
uh now we just need to longtail keywords
9:50
we need to update this with these
9:53
suggestions and then we just need to
9:55
sleep for a certain amount of time
9:57
because if you don't write this line if
10:00
you don't pause you may be banned for
10:03
some time for using so to
10:06
avoid being banned so we put some delay
10:09
to
10:10
and then we return these suggestions in
10:13
a sorted way in a list longtail
10:17
keywords so that's
10:19
all now we get these keywords so after
10:23
that we actually save these keywords
10:26
into a csv file so save to csv and we
10:30
pass this results so now we can define
10:33
this function which will actually save
10:36
this data into a csv file so save to
10:43
csv get the keywords and the file name
10:45
you can give any file name let's suppose
10:47
i say results dot cv
10:52
csv and then we open
10:55
this file name in a right mode and new
11:01
line encoding is your
11:07
utf8 as cs csv file and then
11:13
we write a very basic code using the csv
11:16
module we save this csv file like
11:27
this and then we loop through all the
11:30
keywords like this for each keyword we
11:34
write this inside your csv file
11:44
and then we simply write a line that
11:48
uh all keywords
11:53
saved uh that's all so this is actually
11:55
a python script so if i try to execute
12:00
now let me take another keyword here so
12:03
i will say javascript program 2 i def i
12:07
take a different keyword now so now i
12:09
need to generate all the autocomplete
12:12
suggestions so you will see it will
12:14
suggest all these suggestions so it will
12:17
take around about 5 to 10 seconds to
12:20
extract all the suggestions and
12:22
autocomplete queries on longtail
12:25
keywords and once it is completed it
12:27
will store all these suggestions in a
12:29
csv file you'll see that and this is
12:34
really good guys if you are making
12:35
videos and blogs uh you don't need to
12:38
buy any keyword tool this python script
12:41
will fetch all the suggestions for you
12:43
from google and then these longtail
12:47
keywords will be very much helpful for
12:49
you so you can see that the full script
12:52
guys is given in the description of this
12:53
video if you need the full script you
12:56
can see
12:57
that and thank you very much for
13:00
watching this video please hit that like
13:01
button subscribe the channel and also
13:04
check out my website freemediattools.com
13:08
uh which contains thousands of tools
#Scripting Languages
#Search Engine Optimization & Marketing
#Search Engines
#Web Services