【问题标题】:Python 3 urllib produces TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type strPython 3 urllib 产生 TypeError: POST data should be bytes or an iterable of bytes。它不能是 str 类型
【发布时间】:2015-08-25 23:08:41
【问题描述】:

我正在尝试将工作中的 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.

我还阅读了另外两张票(ticket1ticket2),其中提到了对日期进行编码。

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

我不知道如何使 Python 3 代码工作。你能帮帮我吗?

【问题讨论】:

    标签: python python-2.7 python-3.x urllib2 urllib


    【解决方案1】:

    我在 ZOHO CRM API V2 中使用了 python 请求模块。它没有任何问题。 这是带有 GET 请求的示例工作代码:

    import json
    import requests
    
    # API methods - https://www.zoho.com/crm/developer/docs/api/api-methods.html
    # getrecords API Call
    module_name = 'Deals'
    authtoken = '*****'
    api_url = "https://crm.zoho.com/crm/private/json/"+module_name+"/getRecords?authtoken="+authtoken+"&scope=crmapi&fromIndex=1&toIndex=2"
    
    # GET Request
    request_response = requests.get(
        url=api_url
        )
    print(json.dumps(json.loads(request_response.text), sort_keys=True, indent=4, separators=(",", ": ")))
    json_response = json.loads(request_response.text)
    

    【讨论】:

      【解决方案2】:

      来自docs 请注意,从 urlencode 输出的参数在作为数据发送到 urlopen 之前被编码为字节:

      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)
      

      【讨论】:

        【解决方案3】:

        试试这个:

        url = 'https://www.customdomain.com'
        d = dict(parameter1="value1", parameter2="value2")
        
        f = urllib.parse.urlencode(d)
        f = f.encode('utf-8')
        
        req = urllib.request.Request(url, f)
        

        你的问题在于你处理字典的方式。

        【讨论】:

          猜你喜欢
          • 2020-10-08
          • 2014-03-08
          • 1970-01-01
          • 2019-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-04
          相关资源
          最近更新 更多