urlib encoding

I’m trying to programmatically construct a URL to query a google webservice and struggling to get the right thing. An example of the type of URL i’m trying to generate is as follows:



<br />
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Avatar%20(2009)+site:imdb.com<br />




In the above example, I would like to swap out the title name (Avatar) and the release date (2009). It's also got to be able to handle characters in the title more than just spaces, e.g. "The A-Team". "Harry Potter and the Deathly Hallows: Part 1", etc.

Anyone know how best to do this? I'm pretty sure I should be using urllib2 and the encode function. Just not too sure of the exact specifics!

Thanks in advance

You can use the build in String.Quote:



search_string = String.Quote("%s (%d)"%(title, year), usePlus=False)<br />
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s+site:imdb.com" % search_string



Would this not mean that the "search_string" which is inserted into my url would include a space character, instead of being replaced with a "%20"? That's why i'm keen to use url encoding so that it can handle all the rules regarding translating characters, like space and others, into the appropriate web standard.

When you set usePlus to False, a space becomes %20, according to the [docs](http://dev.plexapp.com/docs/mod_String.html?highlight=quote#String.Quote).


Worked like a dream! Thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.