4

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?

user2950593
  • 9,233
  • 15
  • 67
  • 131
  • Have you gone through some of the other Q&As that are very similar to this? (See the "Related" sidebar.) – glibdud Sep 25 '17 at 18:54
  • @glibdud yes, I have gone through some Q&As that are very similar to this(see last 3 paragraphs of question) – user2950593 Sep 25 '17 at 19:00
  • See [this question](https://stackoverflow.com/questions/24049151/attributeerror-bytes-object-has-no-attribute-timeout) for an explanation of the `AttributeError`. – glibdud Sep 25 '17 at 19:02

2 Answers2

7

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

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