Created
April 28, 2013 19:36
-
-
Save imankulov/5478114 to your computer and use it in GitHub Desktop.
urlunquote sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
urls = [ | |
'http://ru.wikipedia.org/wiki/%D0%91%D0%B5%D0%B4%D1%83%D0%B8%D0%BD%D1%8B', | |
'http://ru.wikipedia.org/wiki/%C1%E5%E4%F3%E8%ED%FB', | |
] | |
def urlunquote(url, guess_charsets=None): | |
if guess_charsets is None: | |
guess_charsets = ['utf-8', 'cp1251'] | |
str_url = urllib.unquote(url) | |
for enc in guess_charsets: | |
try: | |
return str_url.decode(enc) | |
except UnicodeDecodeError: | |
pass # next try ... | |
# fall back to the same url | |
return url | |
for url in urls: | |
print urlunquote(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment