Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 677
User31415926 Avatar asked Jun 25 '26 20:06

User31415926


1 Answers

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)
like image 122
giaosudau Avatar answered Jun 28 '26 08:06

giaosudau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!