gpt4 book ai didi

python - 如何在 scrapy 中避免错误 "TypeError: a bytes-like object is required, not ' str'"

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:20 25 4
gpt4 key购买 nike

    start_urls = ['https://github.com/login']

def parse(self, response):
return scrapy.FormRequest.from_response(response,formdata={'login': 'xx',
'password': 'xx'},callback=self.after_login)

def after_login(self, response):
if "authentication failed" in response.body:
self.logger.info("fail xx %s", response.body)

我引用文档试了上面的代码,但是出现如下错误。

    if "authentication failed" in response.body:
TypeError: a bytes-like object is required, not 'str'

它看起来像response.body 中的二进制文件。有没有办法避免这个错误?

我很好奇,一般情况下,如果登录失败,response.body中是否会显示“authentication failed”?

感谢您阅读我的问题。

最佳答案

您还可以使用 response.text,因为它也会返回正文,但作为 string。因此,您不必将正在搜索的 string 显式转换为 bytes 对象。

if 'authentication failed' in response.text:
#Do something

关于python - 如何在 scrapy 中避免错误 "TypeError: a bytes-like object is required, not ' str'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45074438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com