R
R
raylenmid2020-11-25 11:02:17
Python
raylenmid, 2020-11-25 11:02:17

How to set request encoding in requests?

Good afternoon!
I'm trying to fill out a form and submit a request like this:

# -*- coding: windows-1251 -*-
import requests

payload = {"StFIO": "Иван"}
r = requests.post("http://192.192.192.192/", data=payload)
r.encoding = "windows-1251"
body_data = r.text
print(body_data)

and I get this in the answer (I don’t publish the whole thing, only the problematic part) - value="Р�РІР°РS"

<td width="653" class="style1"><input name="StFIO" type="text" id="StFIO" size="78" maxlength="78" value="�ван"></td>


if I send a request with numbers or Latin - everything is OK, a normal answer and a table with data.
Please tell me how to fix the situation.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-11-25
@AlexPyth

Why are you using "windows-125" encoding to submit the form to the site? Almost always, "utf-8" encoding is used for web applications. Try declaring the "coding" argument with the value "utf-8", and also set the r.encoding variable to the value "utf-8" or remove this line in your script altogether.

A
ALEGOR, 2020-11-25
@ALEGOR1902

Why are you using windows-125 encoding?
Do it like this:

# -*- coding: utf-8 -*-
import requests

payload = {"StFIO": "Иван"}
r = requests.post("http://192.192.192.192/", data=payload)
r.encoding = "utf-8"
body_data = r.text
print(body_data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question