[Tutor] I thank you . . .

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Feb 14 18:59:34 CET 2005



On Mon, 14 Feb 2005, Lobster wrote:

> # Wikipedia single word search engine
> # Monday Feb 14
>
> import webbrowser
>
> sought_word = raw_input("What is your wikipedia search word? ")
> goto_url_location = "http://en.wikipedia.org/wiki/" +  sought_word
> webbrowser.open(goto_url_location)

Hi Lobster,

Very cool!  *grin*

You may want to make the url-building a little more robust.  If the
sought_word contains wacky characters like spaces, the url might not be
"well-formed".  For example, we may want to make sure that ampersands '&'
aren't being treated as HTTP parameter separators.

There's a function called 'urllib.quote_plus()' that should properly
protect wacky characters like ampersands:

###
>>> import urllib
>>> urllib.quote_plus('r&d')
'r%26d'
###

So you may want to quote_plus() the sought_word as you're constructing the
url_location.


Best of wishes to you!



More information about the Tutor mailing list