python - JSON encoding and downloading from a URL -
i'm trying json steam inventory. data :
def downloadstring(url): req = urllib.request.request( url, data=none, headers={ 'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_9_3) applewebkit/537.36 (khtml, gecko) chrome/35.0.1916.47 safari/537.36' } ) f = urllib.request.urlopen(req) return f.read().decode("utf-8")
now, problem encoding. steam using symbols "black star" ('\u2605') causes json part crash :
def test(string): print(json.loads(string)) test(downloadstring(url))
file "c:\python34\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] unicodeencodeerror: 'charmap' codec can't encode character '\u2605' in position 83559: character maps
i don't understand how can else... be... helpful.
json can encoded in of utf-8, utf-16, or utf-32. try decoding utf-16 instead of utf-8.