Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Automate linkedin connections using Python
Next article icon
Google search can be automated using Python script in just 2 minutes. This can be done usingselenium(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

  1. Selenium
    pip install selenium
  2. Chrome browser
  3. ChromedriverDownload the chrome browser fromhere (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 1Asking the user for input.Python3
fromseleniumimportwebdriver# Taking input from usersearch_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 usedbrowser=webdriver.Chrome('chromedriver')foriinrange(1):matched_elements=browser.get("https://www.google.com/search?q="+search_string+"&start="+str(i))
After saving the above script in script.py, run it in the command prompt as:
python script.py
# Method 2Taking search string in the command line itself.Python3
fromseleniumimportwebdriverimportsys# function to convert a list into stringdefconvert(s):str1=""return(str1.join(s))# Assign the arguments passed to a variable search_stringsearch_string=sys.argv[1:]# The argument passed to the program is accepted# as list, it is needed to convert that into stringsearch_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 usedbrowser=webdriver.Chrome('chromedriver')foriinrange(1):matched_elements=browser.get("https://www.google.com/search?q="+search_string+"&start="+str(i))
After saving the above script in script.py, run it in the command prompt as:
python script.py "geeksforgeeks"

How to Automate Google Search Using Selenium in Python
Video Thumbnail

How to Automate Google Search Using Selenium in Python

Video Thumbnail

Automate Google Search using Python Selenium | Python Project

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp