M
M
MaxiZm2022-01-26 13:53:04
Python
MaxiZm, 2022-01-26 13:53:04

I want to use Yandex Balaboba in a Python project, but how?

Tried several options.

import json
import urllib.request

headers = {
    'Content-Type': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_4) AppleWebKit/605.1.15 '
                  '(KHTML, like Gecko) Version/14.1.1 Safari/605.1.15',
    'Origin': 'https://yandex.ru',
    'Referer': 'https://yandex.ru/',
}


API_URL = 'https://zeapi.yandex.net/lab/api/yalm/text3'
payload = {"query": "Ура Хабр", "intro": 0, "filter": 1}
params = json.dumps(payload).encode('utf8')
req = urllib.request.Request(API_URL, data=params, headers=headers)
response = urllib.request.urlopen(req)
print(response.read().decode('utf8'))

It produces the following line:
{"bad_query":0,"error":0,"query":"\u0423\u0440\u0430 \u0425\u0430\u0431\u0440! ","text":"\u0412\u043e\ u0442 \u0438 \u043d\u0430\u0441\u0442\u0430\u043b \u04

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Nesterov, 2022-01-26
@MaxiZm

response_json =  JSON.loads(response.read().decode('utf8'))

A
alekssamos, 2022-01-28
@alekssamos

Yes, you are correct. Just need to decode the JSON.

import json
print( json.loads( response.read().decode('utf8') )["text"] )
# Вот и настал

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question