1

I am trying to convert the following url

http://www.website.com/search/si/1/doctors/Vancouver, BC

to

http://www.website.com/search/si/1/doctors/Vancouver%2C%20BC

I tried

urllib.quote('http://www.website.com/search/si/1/doctors/Vancouver, BC', '')

and it resulted in replacing everything with a percentage sign.

What's the proper way to do this?

user299709
  • 4,922
  • 10
  • 56
  • 88

2 Answers2

1

Use urllib.quote() for the url path leaving everything else as is:

from urllib import quote
from urlparse import urlparse, urlunparse

url = "http://www.website.com/search/si/1/doctors/Vancouver, BC"

scheme, netloc, path, params, query, fragment = urlparse(url)
path = quote(path)
print urlunparse((scheme, netloc, path, params, query, fragment))

prints:

http://www.website.com/search/si/1/doctors/Vancouver%2C%20BC

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0
urllib.quote('www.website.com/search/si/1/doctors/Vancouver, BC')

without the second parameter AND without the protocol part http://. The second parameter is a list of safe characters NOT to be replaced, default is '/', which is OK in your case.

Alexander Gelbukh
  • 2,104
  • 17
  • 29