python3 replace函数报错TypeError: a bytes-like object is required, not ‘str’

海外翻墙免费加速器[入口]

我之前用的python2.7的版本,运行repalce函数没有问题,当我升级为python3.6的时候出现如下报错

很明显报错信息是告诉我 replace函数执行的对象不能是str格式,需要是bytes编码类型的字符串对象。

因此我将cont进行的编码,代码如下

url=’http://www.chenhaifei.com/’
cont=requests.post(url,headers=headers,data=data).content
cont1=cont.decode() #将str转化为bytes类型

实现方法:

使用decode()方法将str转化为bytes类型。

未经允许不得转载:陈海飞博客 » python3 replace函数报错TypeError: a bytes-like object is required, not ‘str’

分享到:更多 ()

海外翻墙免费加速器[入口]