<p>I'd like to know do I normalize a URL in python. </p>

<p>For example, If I have a url string like : "<a href="http://www.example.com/foo" rel="noreferrer">http://www.example.com/foo</a> goo/bar.html"</p>

<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>

Py3
```
```
from urllib.parse import urlparse, urlunparse, quote
def myquote(url):
    parts = urlparse(url)
    return urlunparse(parts._replace(path=quote(parts.path)))

>>> myquote('https://www.example.com/~user/with space/index.html?a=1&b=2')
'https://www.example.com/~user/with%20space/index.html?a=1&b=2'
```
```
Py2
```
```
import urlparse, urllib
def myquote(url):
    parts = urlparse.urlparse(url)
    return urlparse.urlunparse(parts[:2] + (urllib.quote(parts[2]),) + parts[3:])

>>> myquote('https://www.example.com/~user/with space/index.html?a=1&b=2')
'https://www.example.com/%7Euser/with%20space/index.html?a=1&b=2'
```
```
This quotes only the path component.