N
N
nochangez2020-05-16 13:03:32
Python
nochangez, 2020-05-16 13:03:32

How to specify the encoding when returning a response from the site?

I recently started studying telegram bots in python, so far I don’t know all the subtleties, so there was an encoding error when returning a response from the site (I find out the weather by referring to the site). An encoding error is returned:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\lib\encodings\cp1251.py", line 23, in decode
return codecs.charmap_decode(input,self.errors, decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 54: character maps to

I understand this is due to the emoji returned by the site (⛅️ ️+12°C ️→6.1m/s)
Here site access code:

def whatWeather(city):
    url = f'http://wttr.in/{city}'

    parameters = {
        'format': 2,
        'M': ''
    }

    try:
        response = requests.get(url, params=parameters)
    except requests.ConnectionError:
        bot.send_message(message.chat.id, '<сетевая ошибка>')        
    if response.status_code == 200:
        serverAnswer = response.text.strip()
        bot.send_message(message.chat.id, f'Погода в городе {city}: {serverAnswer}')
    else:
        bot.send_message(message.chat.id, '<ошибка на серверах погоды>')


Tell me how to specify the encoding without sacrificing emoji)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zanak, 2020-05-16
@nochangez

Do you indicate the encoding of your sources? It is better to keep scripts in utf8 encoding, which is "native" for the 3rd python branch.
In the server response header with the weather, you can try to see what encoding it indicates and try to re-encode (but first try just specifying the encoding of your scripts, sometimes this is enough)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question