大佬教程收集整理的这篇文章主要介绍了Python 3.6 urllib TypeError:无法将字节连接到str,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
该data
参数应为类似字节的对象。您需要执行以下操作:
urllib.request.urlopen({API_url}, data=bytes(Json.dumps(headers), enCoding="utf-8"))
我正在尝试使用Python 3.6中的urllib从API中提取一些JSON数据。它要求传递标题信息以进行授权。这是我的代码:
import urllib.request,json
headers = {"authorization" : "Bearer {authorization_token}"}
with urllib.request.urlopen("{api_url}",data=headers) as url:
data = json.loads(url.read().decode())
print(data)
和我得到的错误信息:
TraceBACk (most recent call last):
File "getter.py",line 5,in <module>
with urllib.request.urlopen("{url}",data=headers) as url:
File "ApPDAta\Local\Programs\Python\Python36-32\lib\urllib\request.py",line 223,in urlopen
return opener.open(url,data,timeout)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\urllib\request.py",line 526,in open
response = self._open(req,data)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\urllib\request.py",line 544,in _open
'_open',req)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\urllib\request.py",line 504,in _call_chain
result = func(*args)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\urllib\request.py",line 1361,in https_open
context=self._context,check_hostname=self._check_hostName)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\urllib\request.py",line 1318,in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "ApPDAta\Local\Programs\Python\Python36-32\lib\http\client.py",line 1239,in request
self._send_request(method,url,body,headers,encode_chunked)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\http\client.py",line 1285,in _send_request
self.endheaders(body,encode_chunked=encode_chunked)
File "ApPDAta\Local\Programs\Python\Python36-32\lib\http\client.py",line 1234,in endheaders
self._send_output(message_body,line 1064,in _send_output
+ b'\r\n'
TypeError: can't concat bytes to str
Process finished with exit code 1
以上是大佬教程为你收集整理的Python 3.6 urllib TypeError:无法将字节连接到str全部内容,希望文章能够帮你解决Python 3.6 urllib TypeError:无法将字节连接到str所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。