Python3 urllib生成TypeError:POST数据应为字节或字节的iterable。它不能是str类型
我正在尝试将工作的Python 2.7代码转换为Python 3代码,并且从urllib请求模块收到一个类型错误 我使用内置的2to3 Python工具转换下面的工作urllib和urllib2 Python 2.7代码:Python3 urllib生成TypeError:POST数据应为字节或字节的iterable。它不能是str类型,python,python-2.7,python-3.x,urllib2,urllib,Python,Python 2.7,Python 3.x,Urllib2,Urllib,我正在尝试将工作的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.Reque
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.
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
5.
6 req=urllib.request.request(url,data=urllib.parse.urlencode(d))
---->7 f=urllib.request.urlopen(请求)
8 resp=f.read()
urlopen中的C:\Users\Admin\Anaconda3\lib\urllib\request.py(url、数据、超时、cafile、capath、cadefault、上下文)
159其他:
160开瓶器=_开瓶器
-->161返回opener.open(url、数据、超时)
162
163 def安装_开启器(开启器):
C:\Users\Admin\Anaconda3\lib\urllib\request.py处于打开状态(self、fullurl、data、timeout)
459用于self.process_request.get(协议,[])中的处理器:
460 meth=getattr(处理器,meth\u名称)
-->461 req=甲基(req)
462
463响应=自身打开(请求,数据)
C:\Users\Admin\Anaconda3\lib\urllib\request.py在do\u request\中(self,request)
1110 msg=“POST数据应为字节或字节数。”\
1111“它不能是str类型。”
->1112提升类型错误(msg)
1113如果不是请求。则具有_头('Content-type'):
1114 request.add_undirected_头(
TypeError:POST数据应为字节或字节的iterable。它不能为str类型。
我还看了另外两张票(和),上面提到了日期编码
当我将行f=urllib.request.urlopen(req)
更改为f=urllib.request.urlopen(req.encode('utf-8'))
时,我收到了以下错误:AttributeError:'request'对象没有属性'encode'
我一直在思考如何使Python 3代码正常工作。您能帮我吗?请注意,urlencode的参数输出在作为数据发送到urlopen之前被编码为字节:
试试这个:
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)
您的问题在于您处理字典的方式。我将python请求模块与ZOHO CRM API V2一起使用。它工作时没有任何问题。 下面是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)
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)