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編碼,不然編碼的結果不一樣