Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I try to execute the code

BeautifulSoup(html, ...)

it 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")
like image 913
Bryan Avatar asked Apr 19 '16 05:04

Bryan


4 Answers

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.

like image 107
Matvei Nazaruk Avatar answered Oct 16 '22 17:10

Matvei Nazaruk


Try to pass the HTML text directly

soup = BeautifulSoup(html.text)
like image 31
Jorge Avatar answered Oct 16 '22 16:10

Jorge


html.parser is used to ignore the warnings in the page:

soup = BeautifulSoup(html.text, "html.parser")
like image 9
Heba Allah. Hashim Avatar answered Oct 16 '22 17:10

Heba Allah. Hashim


If you're using requests.get('https://example.com') to get the HTML, you should use requests.get('https://example.com').text.

like image 7
Moshe G Avatar answered Oct 16 '22 17:10

Moshe G