Tag Archives: Beautifulsoup 

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')

How to solve the import failure by prompting beautifulsoup under pychar

is usually channelled through IDEA tips, but this time I found that BeautifulSoup failed and suggested

SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(int “Unit tests have failed! .

Internet search attempts, are not consistent, and finally found that the project package management interface import only effective, the following is the process:

File –> Settings (CTRL + Alt + s) – & gt; Project:(Project name) -> Project Interpreter. As shown below:

click “+” and enter Beautifulsoup to find the plugin you want to install.

note: Python2 was chosen to install BeautifulSoup and Python3 was chosen to install bs4.