I am trying to extend a script that opens a web browser and opens URL. I want to use that script and make it to search google for particular query. That's all.

Here's the code that I got. I would appreciate, if you know how to extend it for the search query.

import webbrowser

new=2;

url="http://google.com";

webbrowser.open(url,new=new);

Well, take a look at the url from a search page on google.
I went to google.com and did a search for daniweb.com and got this URL for the results page:
https://www.google.co.uk/?gws_rd=cr&ei=f0UFU-y9CoeK1AW4joGgCw#q=daniweb.com

Looking at that URL, the question mark "?" in the URL indicates the start of the variables that were passed to the search engine in order to generate the results page. The actual search terms are at the end of the url "#q=" indicates the start of the search terms that were used. So it should just be a case of adding "#q=" and your search terms to the google URL.

So if you want to use the script to fire up the browser and search for something, you could have a string variable called googleUrl, which is assigned the value "http://www.google.com/?#q=". This forms the base of the required URL for a search.
Then in your script you could get the user to enter their search terms as a string.
Finally append the users search string to the googleUrl string and open that in your call to webbrowser.open. That should open the browser and perform the search.
e.g.

import webbrowser
new = 2
googleUrl = "http://www.google.com/?#q=";
terms = input("Enter your search terms: ") # or raw_input if using python 2
webbrowser.open(googleUrl + terms, new=new)

Otherwise, if you just want to hard-code the search terms into the script, simply add the terms after '#q=' in the googleUrl string.

Thanks that was helpful. I tried with the raw_input and it worked perfect. I'm off to see if i can do more with this script. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.