TypeError: can't use a string pattern on a bytes-like object, api

本秂侑毒 提交于 2019-12-20 02:02:16

问题


import json
import urllib.request, urllib.error, urllib.parse

Name = 'BagFullOfHoles' #Random player
Platform = 'xone'#pc, xbox, xone, ps4, ps3

url = 'http://api.bfhstats.com/api/playerInfo?plat=' + Platform + '&name=' + Name
json_obj = urllib.request.urlopen(url)
data = json.load(json_obj)
print (data)

TypeError: can't use a string pattern on a bytes-like object

Just recently used 2to3.py and this error or others come up when I try to fix it . Anyone with any pointers?


回答1:


The json_obj = urllib.request.urlopen(url) returns an HTTPResponse object. We need to read() the response bytes in, and then decode() those bytes to a string as follows:

import json
import urllib.request, urllib.error, urllib.parse

Name = 'BagFullOfHoles' #Random player
Platform = 'xone'#pc, xbox, xone, ps4, ps3

url = 'http://api.bfhstats.com/api/playerInfo?plat=' + Platform + '&name=' + Name
json_obj = urllib.request.urlopen(url)
string = json_obj.read().decode('utf-8')
json_obj = json.loads(string)
print (json_obj)



回答2:


Python 3, as you may know, has separate bytes and str types. Reading from a file opened in binary mode will return bytes objects.

The json.load() function only works with files (and file-like objects) opened in text mode (as opposed to binary mode). It appears that urllib.request.urlopen() will return a file-like in binary mode.

Instead of using json.load(), consider reading from the HTTPResponse object and decoding, then passing to json.loads(), like this:

with urllib.request.urlopen(url) as f:
    json_str = f.read().decode()
obj = json.loads(json_str)

Alternatively, you may wish to investigate the requests module.



来源:https://stackoverflow.com/questions/29663749/typeerror-cant-use-a-string-pattern-on-a-bytes-like-object-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!