-1

If I have a variable, lets say, favorite singer, I want to open the web browser and search for that singer. I was planning to use the following code to open Google, but how can I make the users input become the actual search itself?

webbrowser.open('www.google.com')
user2519572
  • 61
  • 1
  • 1
  • 7

1 Answers1

2

To pre-populate the searchbox, use:

import webbrowser
from urllib import urlencode

webbrowser.open('http://www.google.com/?' + urlencode({'q': 'the quick brown fox'}))
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • @Haidro you spotted my deliberate mistake - well done – Jon Clements Jun 28 '13 at 12:44
  • Thanks Jon. I want to use GPS coordinates and put them into google maps. So how would I do that? Here is some code (which doesn't work) webbrowser.open('www.google.com/maps/',longitude_ref) I have longitude_ref stored as a variable, but I want to use it to search. – user2519572 Jun 28 '13 at 12:45
  • @user2519572 Using a comma will add a space. Simply, use `+`. Or even `.format()` – TerryA Jun 28 '13 at 12:47
  • @user2519572 you'll need to find the right url from experimenting with your browser, and then opening that... – Jon Clements Jun 28 '13 at 12:55