当前位置: 首页 > 面试题库 >

Python 3.6 urllib TypeError:无法将字节连接到str

姜俊友
2023-03-14
问题内容

我正在尝试使用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, encode_chunked=encode_chunked)
  File "AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1064, in _send_output
+ b'\r\n'
TypeError: can't concat bytes to str

Process finished with exit code 1

不太确定这里出了什么问题,我没有输入任何字节,所以我不确定为什么会收到错误消息,告诉我无法将字节连接到str。


问题答案:

data参数应为类似字节的对象。您需要执行以下操作:

urllib.request.urlopen({api_url}, data=bytes(json.dumps(headers), encoding="utf-8"))


 类似资料:
  • 问题内容: 事实证明,这是对python的粗略过渡。这里发生了什么?: 输出文件如下所示: 然后我得到这个错误: 问题答案: 返回一个字节串。 在Python 3中,unicode()对象与对象之间没有隐式转换。如果您知道输出的编码,则可以使用它来获取字符串,也可以将要添加的内容转换为

  • 问题内容: 我已经在RHEL7上安装了ES 7.5和Kibana 7.5,但是在启动Kibana并检查UI后,我看到了错误,“ Kibana服务器尚未准备好。” 检查Kibana日志,我发现它没有正确连接到ES。任何帮助表示赞赏! 这是 journalctl –unit kibana 的输出: Elasticsearch.yml Kibana.yml 另外,当我运行 ss -tunlp | gre

  • 我已经在RHEL7上安装了ES 7.5和Kibana 7.5,但是在启动Kibana并检查UI之后,我看到了错误,“Kibana服务器还没有准备好。” 检查Kibana日志,我看到它没有正确连接到ES。感谢任何帮助! 以下是journalctl的输出——单位kibana: 弹性搜索。yml 基巴纳。yml 此外,当我在Kibana启动期间运行ss-tunlp|grep 5601时,我看到Kiban

  • 我正在尝试将MySQL数据库连接到XAMPP。每当我尝试在XAMPP上按start MySQL时,它都不会运行,我会得到一个错误: 这是因为我的病毒防护软件或VPN吗?我将如何更改MySQL和控制面板中的端口?如果有人能给我一些建议或指导我回答与此类似的已回答问题,将不胜感激。谢谢你。

  • null V: BrowserTimeout:0 调试:false DownPollingLimit:2 集线器:http://jenkins主机:jenkins端口 ID:http://node ip:node端口 null 异常的第一行说它无法解析某些东西,但我不能理解什么? 我是不是漏掉了什么?我是第一次做网格设置。

  • 我试图在Spring MVC应用程序中配置ElasticSearch存储库。我使用Spring Data ElasticSearch版本:2.0.7和ElasticSearch Server 2.4.4。 我确信ElasticNode可以工作,下面是示例输出 这是我的测试配置 我得到的错误,应用程序无法连接到弹性节点,堆栈跟踪 我试图从1.7.1, 2.4.4和5.2.1更改弹性搜索节点的版本。没