我知道非标准的%uxxxx方案,但这似乎不是明智的选择,因为该方案已被W3C拒绝.
一些有趣的例子:
心中的人物.如果我在浏览器中输入:
http://www.google.com/search?q=?
Run Code Online (Sandbox Code Playgroud)
然后复制并粘贴它,我看到这个URL
http://www.google.com/search?q=%E2%99%A5
Run Code Online (Sandbox Code Playgroud)
这使得它看起来像Firefox(或Safari)正在这样做.
urllib.quote_plus(x.encode("latin-1"))
'%E2%99%A5'
Run Code Online (Sandbox Code Playgroud)
这是有道理的,除了不能用Latin-1编码的东西,比如三点字符.
…
Run Code Online (Sandbox Code Playgroud)
如果我输入URL
http://www.google.com/search?q=…
Run Code Online (Sandbox Code Playgroud)
进入我的浏览器然后复制粘贴,我明白了
http://www.google.com/search?q=%E2%80%A6
Run Code Online (Sandbox Code Playgroud)
背部.这似乎是做的结果
urllib.quote_plus(x.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为...不能用Latin-1编码.
但后来我不清楚浏览器是如何用UTF-8或Latin-1解码的.
因为这似乎含糊不清:
In [67]: u"…".encode('utf-8').decode('latin-1')
Out[67]: u'\xc3\xa2\xc2\x80\xc2\xa6'
Run Code Online (Sandbox Code Playgroud)
有效,所以我不知道浏览器是如何用UTF-8或Latin-1解码的.
使用我需要处理的特殊字符做什么是正确的?
我想知道什么是最好的方法 - 或者如果标准库有一个简单的方法 - 将域名和路径中的Unicode字符转换为等效的ASCII URL,使用域编码为IDNA和路径% -encoded,根据RFC 3986.
我从用户那里得到一个UTF-8的URL.因此,如果他们输入http://?.ws/?我'http://\xe2\x9e\xa1.ws/\xe2\x99\xa5'的Python.我想要的是ASCII版本:'http://xn--hgi.ws/%E2%99%A5'.
我现在所做的是通过正则表达式将URL拆分为多个部分,然后手动对域进行IDNA编码,并使用不同的urllib.quote()调用单独编码路径和查询字符串.
# url is UTF-8 here, eg: url = u'http://?.ws/?'.encode('utf-8')
match = re.match(r'([a-z]{3,5})://(.+\.[a-z0-9]{1,6})'
r'(:\d{1,5})?(/.*?)(\?.*)?$', url, flags=re.I)
if not match:
raise BadURLException(url)
protocol, domain, port, path, query = match.groups()
try:
domain = unicode(domain, 'utf-8')
except UnicodeDecodeError:
return '' # bad UTF-8 chars in domain
domain = domain.encode('idna')
if port is None:
port = ''
path = urllib.quote(path)
if query is None:
query = '' …Run Code Online (Sandbox Code Playgroud) 我正在尝试编码非ASCII字符,以便我可以将它们放在网址中并使用它们urlopen.问题是我想要一个像JavaScript这样的编码(例如编码ó为%C3%B3):
encodeURIComponent(ó)
'%C3%B3'
Run Code Online (Sandbox Code Playgroud)
但urllib.quote在Python收益ó为%F3:
urllib.quote(ó)
'%F3'
Run Code Online (Sandbox Code Playgroud)
我想知道如何encodeURIComponent在Python中实现像javascript一样的编码,以及如果我可以编码ISO 8859-1像中文这样的非字符.谢谢!
我正在研究为什么我的查询参数中有加+号而不是%20为什么他们有字符串%C3%BC而不是像编码URL那样的ü(UTF-8).
经过2个小时的思考,我的webapp与URL编码标准不兼容,我发现查询字符串的编码方案与URL的编码不同(这里我指的是没有查询字符串的部分).
例子:
那么有人可以告诉我为什么编码方案不同,因为查询参数是URL的一部分?
看到: