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