how to a way avoide error "TypeError: a bytes-like object is required, not 'str'" in scrapy

Viewed 1520
    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)

I tried the above code with reference to the document, but the following error occurred.

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

It looks like binary file in response.body. Is there a way to avoid this error?

and I'm Curious that generally, if login fails, whether "authentication failed" is displayed in response.body?

Thank you for reading my question.

2 Answers

You can also use response.text as it will also return the body but as a string. So you won't have to convert the string you are searching to bytes object explicitly.

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