Python3.x:报错POST data should be bytes, an iterable of bytes
问题:
python3.x:报错
POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
原因:
# 组装GET方法的请求 request = urllib2.Request(url, data, headers)
其中的data需要转为utf-8
解决方案:
# 组装GET方法的请求 #将代码request = urllib2.Request(url, data, headers) 更改为 request = urllib.request.Request(url, data=urllib.parse.urlencode(data).encode(encoding='UTF8'), headers=headers)
本文介绍了解决Python3.x中使用urllib发送POST请求时出现的数据类型错误的方法。当遇到POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.这一错误时,可以通过将字符串数据转换为UTF-8编码的字节流来修复。
2174

被折叠的 条评论
为什么被折叠?



