Python 3,TypeError:需要一个类似字节的对象,而不是'str'

小鸡快跑
async def chuck(self):
        joketype = 'nerdy' if randint(0, 100) < 50 else 'explicit'
        cnjoke = requests.get('http://api.predator.wtf/joke/?arguments={}'.format(joketype),allow_redirects = True).content

        return '{}'.format(str(cnjoke.replace('&quot;', '"').replace('steal', 'steel')))

我正在尝试使用此api来获取chuck norris笑话(不要问笑),但它返回此错误:

return '```{}```'.format(str(cnjoke.replace('&quot;', '"').replace('steal', 'steel')))
TypeError: a bytes-like object is required, not 'str'

我不确定该怎么办。

Yihe
'```{}```'.format(str(cnjoke).replace('&quot;', '"').replace('steal', 'steel'))

或者

'```{}```'.format(cnjoke.decode().replace('&quot;', '"').replace('steal', 'steel'))

content返回字节

type(cnjoke) #bytes

text相反,在您的情况下使用可能会更好

_cnjoke = requests.get('http://api.predator.wtf/joke/?arguments={}'.format(joketype),allow_redirects = True).text
type(_cnjoke) #str

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python 3-TypeError:需要一个类似字节的对象,而不是'str'

Python 2到3“ TypeError:需要一个类似字节的对象,而不是'str'”

Python3 TypeError:需要一个类似字节的对象,而不是“str”

Python3 .replace产生TypeError:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而在Python 3中打开Python 2 Pickle文件时不是'str'

TypeError:在Python3中写入文件时需要一个类似字节的对象,而不是'str'

使用Python 3.5,抛出错误:TypeError:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而不是'str'–用Python保存JSON数据

Python 3.7 TypeError:需要一个类似字节的对象,而不是'str'

python 3.5:“ TypeError:memoryview:需要一个类似字节的对象,而不是'str'”

TypeError:需要一个类似字节的对象,而在python和CSV中不是'str'

TypeError:需要一个类似字节的对象,而在python和CSV中不是'str'

Python错误:TypeError:需要一个类似字节的对象,而不是'str'

Python3写入gzip文件-memoryview:需要一个类似字节的对象,而不是'str'

Python 3 升级,需要一个类似字节的对象,而不是“str”

Python 3.5 TypeError:需要一个类似字节的对象,而不是使用truncate过滤器的'str'

Python 3.6:TypeError:在尝试打印页面中的所有链接时,需要一个类似字节的对象,而不是'str'

TypeError:在Python 3.5.1中使用REST时需要一个类似字节的对象,而不是'str'

类型错误:需要一个类似字节的对象,而不是无服务器和 Python3 的“str”

需要一个类似字节的对象,而不是python中的“ str”错误

使用python计算excel中的重复行,并且我收到错误TypeError:需要一个类似字节的对象,而不是'str'

Python:需要一个类似字节的对象,打印时不需要'str'

需要一个类似字节的对象,而不是'str'

错误:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而不是'str''xxx';'xxx'

如何解决Json错误:TypeError:需要一个类似字节的对象,而不是'str'

子进程“ TypeError:需要一个类似字节的对象,而不是'str'”

TypeError:需要一个类似字节的对象,而不是从文件中读取时的“ str”

TypeError:需要一个类似字节的对象,而在尝试写入csv文件时不是'str'