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
search_string = input ( "Input the URL or string you want to search for:" )
search_string = search_string.replace( ' ' , '+' )
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
def convert(s):
str1 = ""
return (str1.join(s))
search_string = sys.argv[ 1 :]
search_string = convert(search_string)
search_string = search_string.replace( ' ' , '+' )
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"
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 May, 2020
Like Article
Save Article