"ResultSet object has no attribute ‘%s’. You’re probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute ‘text’. You’re probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
查看是否该使用find()方法的地方使用了find_all()方法,当不是一个单独的对象的时候不能使用.text方法
我错误的代码:
最后一行报错了,其实问题在倒数第二行,这里的标签‘a’是一个单独的对象,但我使用了find_all()方法,这样使用是错误的,应改成find()
正确的代码:
import requests
from bs4 import BeautifulSoup
res=requests.get('http://books.toscrape.com/')
html=res.text
soup=BeautifulSoup(html,'html.parser')
items=soup.find('ul',class_='nav nav-list').find_all('li')
for item in items:
name=item.find('a')
print(name.text.strip())