1

I have a problem with url's request:

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

An Exception is raise:

    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)

I guess urllib.request transform the url ('https://www.facebook.com/127573287311337') into 'https://www.facebook.com/Señoras-que-llevan-el-tupper-en-bolsas-de-Chanel-127573287311337/' You can see the letter 'ñ' which is not ascii, so an Exception is "normal".

Somebody has an idea for help me ?

Thank you.

1 Answers1

-2

The solution is quite simple. The URL you enter as an argument should already be encoded as ASCII. What you must do is the argument so it is encoded in ASCII rather than Unicode (which I believe is the default string encoding).

The code should rather be:

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

For this means the URL is encoded in the correct format from the beginning.

For future reference, you should give more detail on what all the variables equal; we really prefer on this website to be able to replicate problems and in this instance (because I did not have the value of timeout) I had a different error raised.

Frogboxe
  • 386
  • 4
  • 12