Python3:
在使用urllib对参数进行URL编码后,调用
formdata = {
"page_limit": "20",
"page_start": "40"
}
data = parse.urlencode(formdata)
request.Request(url, data=data, headers=headers)
时,出现错误:TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
意思是,POST发送的数据必须是字节类型,不能为str类型。
解决方法:
修改url编码为data = parse.urlencode(formdata).encode("UTF8")即可
本文介绍了一个常见的Python urllib库使用误区,当尝试通过POST方法发送数据时,如果数据类型为str而非bytes,会引发TypeError。文章详细解释了如何将数据正确编码为UTF8字节类型,从而避免这一错误。

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



