# urlib encoding

**URL:** <https://forums.plex.tv/t/urlib-encoding/7728>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [February 8, 2011, 5:42pm UTC](https://forums.plex.tv/t/urlib-encoding/7728 "2011-02-08T17:42:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![IanDBird](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/iandbird/32/85993_2.png) [@IanDBird](https://forums.plex.tv/u/IanDBird)\
**Post date:** [February 8, 2011, 5:42pm UTC](https://forums.plex.tv/t/urlib-encoding/7728/1 "2011-02-08T17:42:43Z")

</div>

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:

```auto

<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

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 8, 2011, 6:04pm UTC](https://forums.plex.tv/t/urlib-encoding/7728/2 "2011-02-08T18:04:32Z")

</div>

You can use the build in [String.Quote](http://dev.plexapp.com/docs/mod_String.html?highlight=quote#String.Quote):

```auto

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

```

---

<div class="post-metadata">

**Author:** ![IanDBird](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/iandbird/32/85993_2.png) [@IanDBird](https://forums.plex.tv/u/IanDBird)\
**Post date:** [February 8, 2011, 6:10pm UTC](https://forums.plex.tv/t/urlib-encoding/7728/3 "2011-02-08T18:10:04Z")

</div>

> [@](#):
>
> You can use the build in [String.Quote](http://dev.plexapp.com/docs/mod\_String.html?highlight=quote#String.Quote):  
>   
> 
> ```auto
> 
> 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.

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 8, 2011, 6:45pm UTC](https://forums.plex.tv/t/urlib-encoding/7728/4 "2011-02-08T18:45:56Z")

</div>

> [@](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![IanDBird](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/iandbird/32/85993_2.png) [@IanDBird](https://forums.plex.tv/u/IanDBird)\
**Post date:** [February 8, 2011, 7:49pm UTC](https://forums.plex.tv/t/urlib-encoding/7728/5 "2011-02-08T19:49:46Z")

</div>

> [@](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 8:59pm UTC](https://forums.plex.tv/t/urlib-encoding/7728/6 "2019-12-20T20:59:23Z")

</div>

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