0

I'm trying to print the content of an url as string and I wrote this code to do it

import requests 
r = requests.get('http://example.com')
print r.text

but in this way I get this error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 319-326: ordinal not in range(128)

so I tried to convert in a json string in this way:

print json.dumps(r.text)

The content is printed as string, but I don't think that it's the correct way to do it. Furthermore i see some characters like this

ud83c\uddee\ud83c\uddf9\ud83c\uddea\ud83c\uddfa

and I supposed they are some emoji, so I try to encoding the string in utf-8, but nothing. How can I print the content of the url with the emoji in a correct way ? Thanks

Lx2pwn
  • 313
  • 4
  • 11
  • Or maybe this is better: [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – wwii Dec 28 '19 at 17:16
  • I've already known that I needed to encode the string in utf-8. I missed to write it on the post, sorry. Anyway, I solved it by taking the substring I needed first and encoding it later. – Lx2pwn Dec 29 '19 at 16:36

1 Answers1

0

Use:

r = requests.get('http://example.com')
r.encoding = 'utf-8'
SdahlSean
  • 573
  • 3
  • 13