M
M
Meido no Hitsuji2021-06-27 07:52:50
Python
Meido no Hitsuji, 2021-06-27 07:52:50

Why do requests to "Balabob" return 403? And how to do it right?

I am writing a script for working with the "Balabob" neuron in Python 3.8.5. According to site requests, you can understand that the api is extremely simple there, however, it is not clear why, when requesting a page, the site decides to return 403 to me with a page like this:
60d80214bc7bb412702443.png
I tried to throw a request from both the home and the server - everything is the same.

The query I'm sending is extremely simple:

import requests

result = requests.post('https://zeapi.yandex.net/lab/api/yalm/text3', json={
    'filter': 1,
    'intro': 0,
    'query': "Пошёл я как-то гулять"
}, headers={
    "Content-Type": "application/json"
})

print(result.text)


Fun fact. 3 requests are sent via api to the server: GET "intros" to get the types of text generation, OPTIONS text3 (did not understand why), POST text3 (to send the entered text and generate a response). The funny thing is that intros returns a normal response. The rest of the requests are 403. I also tried to throw GET to get just the page code, but caught 403.

Of the possible solutions, they suggest changing to 3.7.3. I have not tested it myself, but if it really works like this, can you tell me why? And how to transfer it to 3.8, 3.9?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2021-06-27
@MeidoNoHitsuji

Throw it out requestsand use the urllib.request
working version, python 3.8-3.9:

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'))

H
HemulGM, 2021-06-27
@HemulGM

import requests
import json

url = "https://zeapi.yandex.net/lab/api/yalm/text3"

payload = json.dumps({
  "filter": 1,
  "into": 0,
  "query": "Тест был"
})
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question