Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    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.

like image 954
negabaro Avatar asked Mar 09 '23 15:03

negabaro


1 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
like image 149
Nandesh Avatar answered Mar 12 '23 00:03

Nandesh