4

I have got a url in this form - http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show. How can I make it normal url. I have tried using urllib.unquote without much success.

I can always use regular expressions or some simple string replace stuff. But I believe that there is a better way to handle this...

1
  • it's the output of wikipedia api json service... Commented Nov 30, 2010 at 9:48

3 Answers 3

11

urllib.unquote is for replacing %xx escape codes in URLs with the characters they represent. It won't be useful for this.

Your "simple string replace stuff" is probably the best solution.

Sign up to request clarification or add additional context in comments.

1 Comment

urllib.parse.unquote for python> 3.5
5

Have you tried using json.loads from the json module?

>>> json.loads('"http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show"')
'http://en.wikipedia.org/wiki/The_Truman_Show'

The input that I'm showing isn't exactly what you have. I've wrapped it in double quotes to make it valid json.

When you first get it from the json, how are you decoding it? That's probably where the problem is.

Comments

1

It is too childish -- look for some library function when you can transform URL by yourself. Since there are not other visible rules but "/" replaced by "\/", you can simply replace it back:

def unescape_this(url):
    return url.replace(r"\\/", "/")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.