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?

3
  • Have you gone through some of the other Q&As that are very similar to this? (See the "Related" sidebar.) Commented Sep 25, 2017 at 18:54
  • @glibdud yes, I have gone through some Q&As that are very similar to this(see last 3 paragraphs of question) Commented Sep 25, 2017 at 19:00
  • See this question for an explanation of the AttributeError. Commented Sep 25, 2017 at 19:02

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.