Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

327
Visualizações
Check for string in "response.content" raising "TypeError: a bytes-like object is required, not 'str'"

I am trying to see if a sentence is present in the response back from a request.

import requests

r = requests.get('https://www.eventbrite.co.uk/o/piers-test-16613670281')
text = 'Sorry, there are no upcoming events'

if text in r.content: 
   print('No Upcoming Events')

I am getting the following error:

TypeError: a bytes-like object is required, not 'str'

I am not quite sure why this occurring and what the solution would be.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

r.content is a bytes object but text is str, so you can't do the __contains__ (in) check on another directly.

You can easily (re-)define the text object to be a bytestring:

text = b'Sorry, there are no upcoming events'

Now, you can do if text in r.content:.

or you can use r.text to get the str representation directly, and use text as-is (as str).

over 4 years ago · Santiago Trujillo Relatório

0

r.content returns a bytes like object in Python 3.x. To check, do:

>>> type(r.content)
<class 'bytes'>

There are multiple ways to fix your issue. For example:

  1. Decode r.content to string: You can decode it to string as:

    >>> text in r.content.decode()
    False
    
  2. Convert r.content to utf-8 string as:

    >>> text in str(r.content, 'utf-8')
    False
    
  3. Define your text to search as a byte-string. For example:

    text = b'Sorry, there are no upcoming events'
    #      ^  note the `b` here
    

    Now you may simply use it with r.content as:

    >>> text in r.content
    False
    
  4. Use r.text instead of r.content to search for the string, which as the document suggests:

    The text encoding guessed by Requests is used when you access r.text.

    Hence you may just do:

    >>> text in r.text
    False
    
over 4 years ago · Santiago Trujillo Relatório

0

Requests returns a bytes object, you need to convert it to a string before performing the in

Here's a reference about built in types, one of which is bytes https://docs.python.org/3/library/stdtypes.html

the line of code you're looking for is something like,

if text in r.content.decode():
  print('No upcoming events')

by default decode assumes utf-8, you can pass in a different encoding if you need to though.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda