Google search can be automated using Python script in just 2 minutes. This can be done using selenium (a browser automation tool). Selenium is a portable framework for testing web applications. It can automatically perform the same interactions that any you need to perform manually and this is a small example of it. Mastering Selenium will help you automate your day to day tasks like controlling your tweets, Whatsapp texting and even just googling without actually opening a browser in just 15-30 lines of python code. The limits of automation is endless with selenium.
Installation
- Selenium
pip install selenium
- Chrome browser
- Chromedriver
Download the chrome browser from here (choose the version for your system)
After downloading, extract it and then copy the file in the folder of the script.
This can be done in two ways, by taking input from the user and by giving input in the command line itself.
# Method 1
Asking the user for input.
from selenium import webdriver # Taking input from user search_string = input("Input the URL or string you want to search for:") # This is done to structure the string # into search url.(This can be ignored) search_string = search_string.replace(' ', '+') # Assigning the browser variable with chromedriver of Chrome. # Any other browser and its respective webdriver # like geckodriver for Mozilla Firefox can be used browser = webdriver.Chrome('chromedriver') for i in range(1): search_string + "&start;=" + str(i)) |
After saving the above script in script.py, run it in the command prompt as:
python script.py
# Method 2
Taking search string in the command line itself.
from selenium import webdriver import sys # function to convert a list into string def convert(s): str1 = "" return(str1.join(s)) # Assign the arguments passed to a variable search_string search_string = sys.argv[1:] # The argument passed to the program is accepted # as list, it is needed to convert that into string search_string = convert(search_string) # This is done to structure the string # into search url.(This can be ignored) search_string = search_string.replace(' ', '+') # Assigning the browser variable with chromedriver of Chrome. # Any other browser and its respective webdriver # like geckodriver for Mozilla Firefox can be used browser = webdriver.Chrome('chromedriver') for i in range(1): search_string + "&start;=" + str(i)) |
After saving the above script in script.py, run it in the command prompt as:
python script.py "geeksforgeeks"
Recommended Posts:
- Automate linkedin connections using Python
- Automate getter-setter generator for Java using Python
- Automate the Conversion from Python2 to Python3
- Performing Google Search using Python code
- Python | Get a google map image of specified location using Google Static Maps API
- Non blocking wait in selenium using Python
- Python | Automating Happy Birthday post on Facebook using Selenium
- How to access popup login window in selenium using Python
- Flight-price checker using Python and Selenium
- Check CBSE result using Selenium in Python
- Browser Automation Using Selenium
- Selenium Python Tricks
- Hyperlink Induced Topic Search (HITS) Algorithm using Networxx Module | Python
- Voice search Wikipedia using Python
- Speech Recognition in Python using Google Speech API
- Python | Plotting Google Map using gmplot package
- Python | Plotting Google Map using folium package
- Python | Plotting Data on Google Map using pygmaps package
- How to download Google Images using Python
- Uploading files on Google Drive using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : alpeshkaushal

