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

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

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

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

url=’http://rank.chinaz.com/ajaxseo.aspx?t=rankall&callback=jQuery1113004422393396979851_1512442855301′
cont=requests.post(url,headers=headers,data=data).content
cont1=cont.decode() #将str转化为bytes类型
p=cont1.replace(‘jQuery1113004422393396979851_1512442855301’,”)

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

分享到:更多 ()

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    Needed to create you one little bit of word in order to give thanks again just for the pleasant knowledge you’ve documented here. This has been certainly unbelievably generous of you in giving openly what exactly many of us could possibly have made available as an e book to earn some cash for their own end, particularly considering that you could possibly have tried it if you ever considered necessary. Those tactics also served to provide a good way to fully grasp that other people online have a similar eagerness really like my own to grasp way more with reference to this problem. I believe there are many more pleasurable moments in the future for many who examine your website.