negabaro
negabaro

Reputation: 3965

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.

Upvotes: 5

Views: 1770

Answers (2)

Nandesh
Nandesh

Reputation: 4633

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

Upvotes: 6

Martijn Pieters
Martijn Pieters

Reputation: 1123340

response.body is a bytes value, but "authentication failed" is a str. You can't mix the types.

Use a bytes literal:

if b"authentication failed" in response.body:

Upvotes: 2

Related Questions