user2950593
user2950593

Reputation: 9617

UnicodeEncodeError: 'ascii' codec can't encode characters in position 90-96: ordinal not in range(128)

I have this code:

url= 'https://yandex.ru/search/xml?user=uid-2h3232xfhboy&key=03.292922330523:6b4c80ghghghhghgdsfdsfds4c4b4a7872fb7d2bb04bfdgbb02b76c3d&query='
key = "абс"
 url = url + key
        print(url)
        xml = urllib.request.urlopen(url).read()

But I got an error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 90-96: ordinal not in range(128)

What do I do?

I tried to do url= url.encode("utf-8") But didn't help. Got this error:

AttributeError: 'bytes' object has no attribute 'timeout'

I tried to do this: url = u''.join((self.ya_url, key)).encode('utf-8') As suggested here: UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

But got the same error

AttributeError: 'bytes' object has no attribute 'timeout'

What do I do?

Upvotes: 4

Views: 2971

Answers (2)

This method work for me (i use Pycharm ide) . you go to client.py , then change the request.encode('ascii') to request.encode('utf-8) or any encoder you want . Now it should be work with no problem

Edit: you need to change the source file in order to use utf character in url . in request.encode , it has been hard code to ascii

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1120848

You can't use non-ASCII characters in a URL. You need to quote your key value appropriately:

import urllib.parse

url= 'https://yandex.ru/search/xml?user=uid-2h3232xfhboy&key=03.292922330523:6b4c80ghghghhghgdsfdsfds4c4b4a7872fb7d2bb04bfdgbb02b76c3d&query='
key = "абс"
quoted = urllib.parse.quote(key)
url = url + quoted

Upvotes: 7

Related Questions