Search code examples
pythonpython-webbrowserpyperclip

Python - How to search in google without typing url


I tried to take the input from the user, copy it, and then open google and paste the input.

this is my code:

import webbrowser

import pyperclip

question = input("search for: ")

pyperclip.copy(question)

url = 'http://www.google.com/'

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

webbrowser.get(chrome_path).open(url) 

pyperclip.paste()

Solution

  • 
    import webbrowser
    
    
    question = input("search for: ")
    question = question.replace(' ', '+') # replace <space> with +
    
    
    url = 'https://www.google.com/search?q=' + question # google search url
    
    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
    
    webbrowser.get(chrome_path).open(url) 
    

    You don't need to paste the url. When you search a url something like "hello world" in the browser the url is
    enter image description here
    The spaces are replaced with + sign.