【问题标题】:UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in position 23: ordinal not in range(128) [duplicate]UnicodeEncodeError:“ascii”编解码器无法在位置 23 编码字符“\xb0”:序数不在范围内(128)[重复]
【发布时间】:2017-12-18 15:32:22
【问题描述】:

我正在尝试从包含如下特殊字符的链接下载图像: imageUrl = 'https://www.residentadvisor.net/images/labels/3000°records.jpg'

request_=urllib.request.Request(imageUrl,None,headers) #The assembled request
print(request_)
response = urllib.request.urlopen(request_) #store the response

但在尝试下载时出现以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in position 23: ordinal not in range(128)

因为Traceback 太大了,所以请在这个网址找到它:

for Traceback click here

UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-52-d0b0a8f766c9> in <module>()
    135             request_=urllib.request.Request(imageUrl,None,headers) #The assembled request
    136             print(request_)
--> 137             response = urllib.request.urlopen(request_) #store the response
    138 #create a new file and write the image

    /Users/-/anaconda/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221     else:
222         opener = _opener
--> 223     return opener.open(url, data, timeout)
224 
225 def install_opener(opener):

/Users/-/anaconda/ lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
524             req = meth(req)
525 
--> 526         response = self._open(req, data)
527 
528         # post-process response

/Users/-/anaconda/lib/python3.6/urllib/request.py in _open(self, req, data)
542         protocol = req.type
543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
545         if result:
546             return result

/Users/-/anaconda/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
502         for handler in handlers:
503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
505             if result is not None:
506                 return result

/Users/-/anaconda/lib/python3.6/urllib/request.py in https_open(self, req)
1359         def https_open(self, req):
1360             return self.do_open(http.client.HTTPSConnection, req,
-> 1361                 context=self._context, check_hostname=self._check_hostname)
1362 
1363         https_request = AbstractHTTPHandler.do_request_

/Users/-/anaconda/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1316             try:
1317                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318                           encode_chunked=req.has_header('Transfer-encoding'))
1319             except OSError as err: # timeout error
1320                 raise URLError(err)

/Users/-/anaconda/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1237                 encode_chunked=False):
1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
1240 
1241     def _send_request(self, method, url, body, headers, encode_chunked):

/Users/-/anaconda/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1248             skips['skip_accept_encoding'] = 1
1249 
-> 1250         self.putrequest(method, url, **skips)
1251 
1252         # chunked encoding will happen if HTTP/1.1 is used and either

/Users/-/anaconda/lib/python3.6/http/client.py in putrequest(self, method, url, skip_host, skip_accept_encoding)
1115 
1116         # Non-ASCII characters should have been eliminated earlier
-> 1117         self._output(request.encode('ascii'))
1118 
1119         if self._http_vsn == 11:

UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in position 23: ordinal not in range(128)

【问题讨论】:

  • 为什么说traceback太大了?复制/粘贴应该可以正常工作。我不会为了查看有关您的问题的基本信息而单击随机的 Internet 链接。
  • 因为我无法以当前形式发布带有所有回溯的问题...... s/o 告诉我添加更多文本,因为如果我格式化回溯,它似乎只是代码跨度>
  • 如果你愿意,我可以将其添加为图像?
  • 继续添加不带格式的文本,有人会过来为您设置格式。愚蠢的 SO 和他们对内容质量的要求往往适得其反。
  • 现在得到'帖子似乎包含格式不正确的代码......',将尝试以不同的方式进行

标签: python unicode urllib python-unicode


【解决方案1】:

urllib.request 需要一个正确的 url 转义 url。

在这种情况下,正确转义的 url 是:

imageUrl = 'https://www.residentadvisor.net/images/labels/3000%C2%B0records.jpg'

如果您正在处理可能编码不佳的 url,一个可以帮助您正确编码它们的库是yelp_uri.encoding.recode_uri。完全披露:我为这个库做出了贡献。

我使用以下代码获取正确编码的 url:

from yelp_uri.encoding import recode_uri
imageUrl = recode_uri(imageUrl)

【讨论】:

  • 感谢您的披露和图书馆......就像一个魅力;)
猜你喜欢
  • 2018-06-29
  • 1970-01-01
  • 2018-09-29
  • 2013-11-18
  • 2016-10-06
  • 2020-04-04
  • 2019-11-10
  • 2021-05-14
  • 2023-03-03
相关资源
最近更新 更多