代码如下:
rom bs4 import BeautifulSoup
import requests
url='XXX'
web=requests.get(url)
soup=BeautifulSoup(web,'lxml')
print(soup)
就这几行,出错是:
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
为何解决呀??
答
soup=BeautifulSoup(web,'lxml')
这个地方有错,web在这里是一个response对象,无法用BeautifulSoup解析,如果要解析,解析对象应该是web.content,所以正确的写法是
soup=BeautifulSoup(web.content,'lxml')