How To make a Bot in That can do google search!!

A Bot in That can do google search!!

Knowledge multiplies when shared!

So without any further ado let's get started!

Installing Packages:

First part in this tutorial is to install the necessary packages. In this case we will use Google API :

  • beautifulsoup4
  • google

Here google is the main module and beautifulsoup4 is the dependency so both are necessary!!

Importing the Module:

So now that we have installed everything required, then let's import them! So go to your bot repl and write:

     from googlesearch import search 
except ImportError:
     print("No module found!")

Here we have given a try/except statement in-case if the module was not successfully installed or was not found!

Making the function:

Now we have everything on the go! So let make the function!

async def find(ctx,*, query):

So here I have made the command using bot.command()
Then started the function with async def find(ctx,*,query):
Here I have given the function name Find but you can give it anything!

Now we can take the writer's name to style out our function, so that can be done with:

author =

Then we will send a message to the user that his/her information is being searched! So to that we will do:

await"Here are the links related to your question {author} !") 

And after that in-case the result takes a lot of time to be retrieved then we can make a code that will make a typing effect, like when a human user types there! So:

async with ctx.typing(): 

This will prevent the user from thinking that the bot is not working!

So now the main part comes! How will we search the net?
As you know that we have import search from googlesearch module so to make a real google search we have to put so parameters this can be opinion based situation!
So the code is:

for j in search(query, tld="", num=10, stop=10, pause=2):

yeah you got that right it is made in for loop so now let me explain you each parameters importance!

query : query string that we want to search for.
tld : tld stands for top level domain which means we want to search our result on or or some other domain.
lang : lang stands for language.
num : Number of results we want.
start : First result to retrieve.
stop : Last result to retrieve. Use None to keep searching forever.
pause : Lapse to wait between HTTP requests. Lapse too short may cause Google to block your IP. Keeping significant lapse will make your program slow but its safe and better option.
Return : Generator (iterator) that yields found URLs. If the stop parameter is None the iterator will loop forever.

So above I have provided you the code if you want multiple result like 5-10 per query but if you want one result then you can simply just change the num, stop values to your desired numbers!
Now to print the result we will await the result and send it to the channel (Note: put this inside for loop!), like:

await ctx.send(f"\n:point_right: {j}") 

Thus the program is done!

This was my first tutorial!
Hope you like this tutorial
Leave a comment in case of any doubt, every comment is accepted!
If you think that this post helped you, It will be my pleasure!

You are viewing a single comment. View All
maxwon (0)

@bot.command(): name 'bot' is not defined???


firstly have u given the colon after @bot.command() in ur code also or u just wrote it here?
Otherwise, check here they have explained here nicely: