Typeerror: object of type ‘response’ has no len() why?

The code is as follows:

rom bs4 import BeautifulSoup
import requests
url='XXX'
web=requests.get(url)
soup=BeautifulSoup(web,'lxml')
print(soup)

On these lines, the error is:

E:\Python\Python35-32\python.exe C:/Users/ty/PycharmProjects/untitled3/src/Reptile.py
Traceback (most recent call last):
File "C:/Users/ty/PycharmProjects/untitled3/src/Reptile.py", line 7, in <module>
soup=BeautifulSoup(web,'lxml')
File "E:\Python\Python35-32\lib\site-packages\beautifulsoup4-4.5.1-py3.5.egg\bs4\__init__.py", line 192, in __init__
TypeError: object of type 'Response' has no len()


Process finished with exit code 1

Why??

answer


soup=BeautifulSoup(web,'lxml')

There is a mistake in this place. The web here is a response object, which can’t be parsed by using beautiful soup. If you want to parse, the parsing object should be web.content So the correct way to write it is

soup=BeautifulSoup(web.content,'lxml')

Read More: