Search code examples
pythonpython-requestsads

requests.Response.text shows odd symbols


I use Python's request library to access (public) ads.txt files:

import requests
r = requests.get('https://www.sicurauto.it/ads.txt')
print(r.text)

This works fine in most cases, but the text from the URL above begins with some strange symbols:

> google.com, [...]

If I open the URL in my browser, I do not see these three symbols; the text begins with google.com, [...] I am a beginner when it comes to encodings and web protocols ... where might these odd symbols come from?


Solution

  • You need to specify your encoding (in r.encoding) before calling r.text:

    import requests
    r = requests.get('https://www.sicurauto.it/ads.txt')
    r.encoding = 'utf-8-sig'  # specify UTF-8-sig encoding
    print(r.text)