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

Python 3 urllib产生TypeError:POST数据应为字节或字节可迭代。不能是str类型

林昱
2023-03-14
问题内容

我正在尝试将有效的Python 2.7代码转换为Python 3代码,并且从urllib请求模块收到类型错误。

我使用内置的2to3 Python工具来转换以下工作的urllib和urllib2 Python 2.7代码:

import urllib2
import urllib

url = "https://www.customdomain.com"
d = dict(parameter1="value1", parameter2="value2")

req = urllib2.Request(url, data=urllib.urlencode(d))
f = urllib2.urlopen(req)
resp = f.read()

2to3模块的输出为以下Python 3代码:

import urllib.request, urllib.error, urllib.parse

url = "https://www.customdomain.com"
d = dict(parameter1="value1", parameter2="value2")

req = urllib.request.Request(url, data=urllib.parse.urlencode(d))
f = urllib.request.urlopen(req)
resp = f.read()

运行Python 3代码时,会产生以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-206954140899> in <module>()
      5 
      6 req = urllib.request.Request(url, data=urllib.parse.urlencode(d))
----> 7 f = urllib.request.urlopen(req)
      8 resp = f.read()

C:\Users\Admin\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    159     else:
    160         opener = _opener
--> 161     return opener.open(url, data, timeout)
    162 
    163 def install_opener(opener):

C:\Users\Admin\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
    459         for processor in self.process_request.get(protocol, []):
    460             meth = getattr(processor, meth_name)
--> 461             req = meth(req)
    462 
    463         response = self._open(req, data)

C:\Users\Admin\Anaconda3\lib\urllib\request.py in do_request_(self, request)
   1110                 msg = "POST data should be bytes or an iterable of bytes. " \
   1111                       "It cannot be of type str."
-> 1112                 raise TypeError(msg)
   1113             if not request.has_header('Content-type'):
   1114                 request.add_unredirected_header(

TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

我还阅读了另外两个票证(ticket1和ticket2),其中提到了对日期进行编码。

当我将行更改为时f = urllib.request.urlopen(req)f = urllib.request.urlopen(req.encode('utf-8'))收到以下错误:AttributeError: 'Request' object has no attribute 'encode'

我对如何使Python 3代码工作感到困惑。请你帮助我好吗?


问题答案:

From the
docs Note that params output from urlencode is encoded to bytes before
it is sent to urlopen as data:

data = urllib.parse.urlencode(d).encode("utf-8")
req = urllib.request.Request(url)
with urllib.request.urlopen(req,data=data) as f:
    resp = f.read()
    print(resp)


 类似资料:
  • 我试图将工作的Python 2.7代码转换成Python 3代码,我从urllib请求模块收到一个类型错误。 我使用内置的2to3 Python工具来转换下面的工作urllib和urllib2 Python 2.7代码: 2to3模块的输出是以下Python 3代码: 当运行Python 3代码时,会产生以下错误: 我还读了另外两张票(ticket1和ticket2),其中提到了日期编码。 当我更

  • 问题内容: 我已经将脚本从Python 2.7转换为3.2,并且有一个错误。 在最后一行,我得到了这个错误: 我已经安装了Python 3.2,并且已经安装了lxml-2.3.win32-py3.2.exe。 在Python 2.7上可以使用。 问题答案: 输出文件应处于二进制模式。

  • 问题内容: 据我了解,Java编译器生成“字节代码”,而不是“目标代码”。首先,这是正确的吗? 而且,这就是我的书所说的,我想知道为什么这是正确的。字节码和目标码有什么区别? 问题答案: 字节代码只是Java虚拟机的“目标代码”。它不是 本机 代码(例如x86)。老实说,这些天我很少听到“目标代码”一词-用更具体的术语讲通常更清晰。

  • 今天面试字节前端,然后面试官问我你这个项目是自己从0到1搭建的嘛?我说从网上拉下来的模板,然后他问那你对这个模板中每一个babel和plugin还有相应的loader都清楚了解嘛?我说只会常见的 虽然我知道我应该挂了,但是还是忍不住问面试官,反问的对话如下: 我:今年校招要求这么高的嘛? 面试官:今年字节对校招生的要求就是很高,希望招一个进入就能干活的,对前端充满热爱的同学 我:那如果我现在不会,

  • 问题内容: 我如何迭代字节数组中的位? 问题答案: 你必须写自己的实施历时字节数组,然后创造价值,其记忆中的当前索引的字节数组 和 当前字节中的当前索引。然后像这样的实用方法会派上用场: (范围为0到7)。每次调用时,您都必须在当前字节中增加位索引,并且在到达“第9位”时也必须增加字节数组中的字节索引。 这并不 难 -有点痛苦。让我知道您是否需要示例实现…

  • 问题内容: 我一直在寻找一种从python脚本运行外部进程并在执行期间打印其stdout消息的方法。 下面的代码有效,但是在运行时不输出标准输出。退出时,出现以下错误: sys.stdout.write(nextline)TypeError:必须为str,而不是字节 我正在使用python 3.3.2 问题答案: Python 3处理字符串有些不同。最初,字符串只有一种类型:。上世纪90年代,当u