TypeError: can't use a string pattern on a byt

2020-04-10 22:45发布

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?

2条回答
ゆ 、 Hurt°
2楼-- · 2020-04-10 23:37

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)
查看更多
可以哭但决不认输i
3楼-- · 2020-04-10 23:46

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.

查看更多
登录 后发表回答