问题描述
解码被base64编码过的字段信息,在base64编码解码工具中正常解码,但在python下的模块base64、binascii中的b64decode、a2b_base64等方法中出现解码报错
报错信息如下
1 2 3 4 5 6 7 8 9 10 11 12 13
| --------------------------------------------------------------------------- Error Traceback (most recent call last) <ipython-input-11-787bc11958b4> in get_proxies(urls) 14 try: ---> 15 raw = base64.b64decode(response) 16 except Exception as r:
c:\program files\python3\lib\base64.py in b64decode(s, altchars, validate) 86 raise binascii.Error('Non-base64 digit found') ---> 87 return binascii.a2b_base64(s) 88
Error: Incorrect padding
|
解决方案
python中的base64是4个4个的读取的,所以待解码的字段应当为4的倍数,不足添‘=’
1 2
| a = a + '=' * (4 - len(a) % 4) if len(a) % 4 != 0 else a
|