son格式的string对象转变成dict对象操作
- content=eval(content)#json字典转化
Python3不能使用urllib2
- 直接使用urllib.request替换urllib2就可以了
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=PTi4WZjaMjXgrxqaH7OVOG1c&client_secret=8fpp9Hw9wMKGrtGIitNox8vDfFZKMNNA'
request = urllib2.Request(host) #python3执行会报错
request = urllib.request.Request(host)#替换urllib2
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
content=content.decode("utf-8")#把返回的二进制转为str
content=eval(content)#json字典转化
print (type(content))
print(content)
access_token= content['access_token']
print(access_token)
params = urllib.parse.urlencode(params).encode(encoding='UTF8')要先对参数进行utf-8的编码之后,才能进行url编码,否则编码的结果不同