【发布时间】: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 太大了,所以请在这个网址找到它:
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