Найти - Пользователи
Полная версия: [Python 3] URLopen и UTF8 пути
Начало » Network » [Python 3] URLopen и UTF8 пути
1
an1s1
Есть сайт, на котором лежит страница, но вот ее название в русской раскладке(UTF8), на что URLopen говорит, мол незя, не вмещается в 128 значений ASCII. Что с этим делать? Заранее благодарен.
an1s1
File "***\parser.py", line 25, in getPageText
    return str(urlopen(url).readall())
  File "C:\Python32\lib\urllib\request.py", line 138, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python32\lib\urllib\request.py", line 369, in open
    response = self._open(req, data)
  File "C:\Python32\lib\urllib\request.py", line 387, in _open
    '_open', req)
  File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
    result = func(*args)
  File "C:\Python32\lib\urllib\request.py", line 1156, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python32\lib\urllib\request.py", line 1136, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python32\lib\http\client.py", line 964, in request
    self._send_request(method, url, body, headers)
  File "C:\Python32\lib\http\client.py", line 992, in _send_request
    self.putrequest(method, url, **skips)
  File "C:\Python32\lib\http\client.py", line 856, in putrequest
    self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-11: ordinal not in range(128)
fata1ex
response = urlopen()
response.decode()

Или у вас в урле юникод? Тогда http://python.su/forum/topic/6750/
an1s1
fata1ex
response = urlopen()
response.decode()
Не-а, проблема, судя по всему, при декодировании url
File "***\parser.py", line 25, in getPageText
    return str(urlopen(url).decode())
  File "C:\Python32\lib\urllib\request.py", line 138, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python32\lib\urllib\request.py", line 369, in open
    response = self._open(req, data)
  File "C:\Python32\lib\urllib\request.py", line 387, in _open
    '_open', req)
  File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
    result = func(*args)
  File "C:\Python32\lib\urllib\request.py", line 1156, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python32\lib\urllib\request.py", line 1136, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python32\lib\http\client.py", line 964, in request
    self._send_request(method, url, body, headers)
  File "C:\Python32\lib\http\client.py", line 992, in _send_request
    self.putrequest(method, url, **skips)
  File "C:\Python32\lib\http\client.py", line 856, in putrequest
    self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-11: ordinal not in range(128)
an1s1
Попробывал так:
>>>page=getPageText("***/Опт".encode("ascii"))
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    page=getPageText("***/Опт".encode("ascii"))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 30-36: ordinal not in range(128)
an1s1
fata1ex
response = urlopen()
response.decode()

Или у вас в урле юникод? Тогда http://python.su/forum/topic/6750/
Не сразу приметил, спасибо, оно.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB