BeautifulSoup: object of type 'Response' has no len()

Bryan picture Bryan · Apr 19, 2016 · Viewed 53.6k times · Source

Issue: when I try to execute the script, BeautifulSoup(html, ...) gives the error message "TypeError: object of type 'Response' has no len(). I tried passing the actual html as a parameter, but it still doesn't work.

import requests

url = 'http://vineoftheday.com/?order_by=rating'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html, "html.parser")

Answer

Matvei Nazaruk picture Matvei Nazaruk · Apr 19, 2016

You are getting response.content. But it return response body as bytes (docs). But you should pass str to BeautifulSoup constructor (docs). So you need to use the response.text instead of getting content.