Python3.x:报错POST data should be bytes, an iterable of bytes


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) 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM