Reputation: 110093
I have the following url, that I have:
https://www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr. Avila/1/9
I would like to encode it so that it looks like a normal url, but is valid. For example:
https://www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr.%20Avila/1/9
However, if I use the standard urllib.quote
it encodes everything:
>>> urllib.quote('https://www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr. Avila/1/9')
'https%3A//www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr.%20Avila/1/9'
Is there a python method that will encode only the non-standard parts of the url, i.e., excluding the forward slashes and colons, etc?
Upvotes: 2
Views: 376
Reputation: 1367
An example, for Python2
In [45]: scheme, netloc, path, query, fragment = urllib2.urlparse.urlsplit(url)
In [60]: urllib2.urlparse.urlunsplit([scheme, netloc, urllib.quote(path), query, fragment])
Out[60]: 'https://www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr.%20Avila/1/9'
Upvotes: 1
Reputation: 24945
You want the 'safe' argument:
If you are on Python3, using urllib.parse
:
import urllib.parse
x ='https://www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr. Avila/1/9'
urllib.parse.quote(x, safe = ':/')
out:
'https://www.verizon.com/OnDemand/TVShows/TVShowDetails/Sr.%20Avila/1/9'
Upvotes: 2