python3 urrlib.request.urlopen UnicodeEncodeError:'ascii'

时间:2015-10-21 15:49:52

标签: python-3.x encode urllib urlopen

我的网址请求有问题:

req = 'https://www.facebook.com/127573287311337'
handler = urllib.request.urlopen(req, timeout=30)

提出异常:

    Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/lib/MyRequests.py", line 104, in str_from_url
    handler = urllib.request.urlopen(req, timeout=timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 461, in open
    response = meth(req, response)
  File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.4/urllib/request.py", line 493, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 676, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 461, in open
    response = meth(req, response)
  File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.4/urllib/request.py", line 493, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 676, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 455, in open
    response = self._open(req, data)
  File "/usr/lib/python3.4/urllib/request.py", line 473, in _open
    '_open', req)
  File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 1273, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.4/urllib/request.py", line 1232, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.4/http/client.py", line 1065, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.4/http/client.py", line 1093, in _send_request
    self.putrequest(method, url, **skips)
  File "/usr/lib/python3.4/http/client.py", line 957, in putrequest
    self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)

我想urllib.request会将网址('https://www.facebook.com/127573287311337')转换为'https://www.facebook.com/Señoras-que-llevan-el-tupper-en-bolsas-de-Chanel-127573287311337/' 你可以看到字母'ñ'不是ascii,所以Exception是“正常的”。

有人有帮助我的想法吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

解决方案非常简单。您作为参数输入的URL应该已经编码为ASCII。您必须做的是参数,因此它以ASCII而不是Unicode(我认为是默认的字符串编码)进行编码。

代码应该是:

    url = 'https://www.facebook.com/127573287311337'.encode("ascii")
    handler = urllib.request.urlopen(url, timeout=timeout)

这意味着URL从头开始以正确的格式编码。

为了将来参考,您应该详细说明所有变量的相同之处;我们真的更喜欢这个网站能够复制问题,在这种情况下(因为我没有超时的值)我引发了不同的错误。