A
A
Artemy Strukov2021-01-28 15:48:25
Python
Artemy Strukov, 2021-01-28 15:48:25

How to add Russian character encoding to the requests library request (Python and Geocoder API from Yandex)?

There is an API Geocoder from Yandex, in which there is a receipt of address coordinates by reference. Request example: . It contains Russian letters and requests does not get along with them. My request function:

def requestsGet():
    r = requests.get('https://https://geocode-maps.yandex.ru/1.x/?apikey=7cf0c3a3-4ae5-4e20-8250-34d99c8eb365=Москва,+Тверская+улица,+дом+7', params={'format': 'json'})
    print('Url запроса:', r.url)
    return r.json()


As a result, the request becomes unreadable:
geocode-maps.yandex.ru/1.x/?apikey=7cf0c3a3-4ae5-4e20-8250-34d99c8eb365=%D0%9C%D0%BE%D1%81%D0%BA%D0 %B2%D0%B0,+%D0%A2%D0%B2%D0%B5%D1%80%D1%81%D0%BA%D0%B0%D1%8F+%D1%83%D0%BB%D0 %B8%D1%86%D0%B0,+%D0%B4%D0%BE%D0%BC+7&format=json

And requests, surprisingly, throws an error:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='https', port=443): Max retries exceeded with url: //geocode-maps.yandex.ru/1.x/?apikey=7cf0c3a3-4ae5-4e20-8250- 34d99c8eb365=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0,+%D0%A2%D0%B2%D0%B5%D1%80%D1%81%D0 %BA%D0%B0%D1%8F+%D1%83%D0%BB%D0%B8%D1%86%D0%B0,+%D0%B4%D0%BE%D0%BC+7&format=json (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))


Link to the documentation on the format of the request API Geocoder from Yandex

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-01-28
@Kavrese

The resulting result

%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0,+%D0%A2%D0%B2%D0%B5%D1%80%D1%81%D0%BA%D0%B0%D1
and there is an encoding. This is absolutely valid syntax.
https://www.urlencoder.org/
screenshot
6012c8831940b501535859.jpeg

And requests, surprisingly, throws an error:

Of course, because you already indicated the protocol twice
https://https:// geocode-maps.yandex.ru

A
Andy_U, 2021-01-28
@Andy_U

You have "&geocode=" missing from your query string. And apikey was published in vain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question