[英]How can I create a percent encoded URL from complete URL?
The input url is mixed ascii and multi byte code. 输入的URL是混合的ascii和多字节代码。 And I can't change this strings.
而且我无法更改此字符串。 I would like to download this url, but an error occured
ordinal not in range(128)
. 我想下载此URL,但是
ordinal not in range(128)
发生了错误。
input_url = "http://sample.jp/api?v1=aaa&v2=日本語&v3=ccc"
req = urllib.request.Request(input_url)
resp = None
try:
resp = urllib.request.urlopen(req)
except UnicodeEncodeError as e:
print(e.reason) # I had an error `ordinal not in range(128)`
return resp
First, I tried urllib.parse.quote()
but the result is http%3a%2f%2fsample%2ejp%2fapi%3fv1%3daaa%26v2%3d%93%fa%96%7b%8c%ea%26v3%3dccc
. 首先,我尝试了
urllib.parse.quote()
但结果是http%3a%2f%2fsample%2ejp%2fapi%3fv1%3daaa%26v2%3d%93%fa%96%7b%8c%ea%26v3%3dccc
。 I had another error ValueError: unknown url type
. 我还有另一个错误
ValueError: unknown url type
。 How can I resolve this problem? 我该如何解决这个问题? Or do you have some ideas?
还是您有一些想法?
You need to encode those parameters to UTF-8 bytes and the bytes toURL percent encoding. 您需要将这些参数编码为UTF-8字节,并将字节编码为URL百分比编码。 You can do all this with the
urllib.parse
module : 您可以使用
urllib.parse
模块完成所有这些urllib.parse
:
from urllib.parse import urlparse, parse_qs, urlencode
parsed = urlparse(input_url)
query = parse_qs(parsed.query)
fixed_url = parsed._replace(query=urlencode(query, doseq=True)).geturl()
Demo: 演示:
>>> from urllib.parse import urlparse, parse_qs, urlencode
>>> input_url = "http://sample.jp/api?v1=aaa&v2=日本語&v3=ccc"
>>> parsed = urlparse(input_url)
>>> query = parse_qs(parsed.query)
>>> parsed._replace(query=urlencode(query, doseq=True)).geturl()
'http://sample.jp/api?v1=aaa&v2=%E6%97%A5%E6%9C%AC%E8%AA%9E&v3=ccc'
>>> import urllib.request
>>> urllib.request.urlopen(_)
<http.client.HTTPResponse object at 0x108f0f7b8>
A combination of urllib, and urlparse should do it for you: urllib和urlparse的组合应该为您做到这一点:
>>> urllib.urlencode(urlparse.parse_qsl(urlparse.urlparse(input_url).query))
'v1=aaa&v2=%E6%97%A5%E6%9C%AC%E8%AA%9E&v3=ccc'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.