1

I am making a request using requests lib, as can be seen in the content the encoding for page is utf-8, also the default used by requests is utf-8 as seen in r.encoding, but why is this showing unicode error while reading text.

r = requests.get(url, auth=('username', 'password'))

print r.status_code
print r.encoding
print r.content

print r.text

output:

200
UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Sign In</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/><meta http-equiv="content-type" content="text/html; charset=utf-8"/>.............

Traceback (most recent call last):
  File "E:\Python practise programms\reuters.py", line 18, in <module>
    print r.text
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2307-2309: ordinal not in range(128)
garg10may
  • 4,424
  • 6
  • 35
  • 70
User
  • 453
  • 4
  • 17
  • 1
    It's assuming that your console is ASCII, thus it can't print any Unicode characters with a codepoint above 0x7f. – Mark Ransom Feb 18 '15 at 23:48

1 Answers1

1

This was due to sublime text not supporting utf-8 by default. I was able to get the output in python IDLE

User
  • 453
  • 4
  • 17