故障排除AttributeError:“ ResultSet”对象没有属性“ findAll”
问题内容:
我正在尝试解析http://www.ted.com/talks页面以获取演讲的所有名称。使用BeautifulSoup,这是我所拥有的:
import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen("http://www.ted.com/talks")
soup = BeautifulSoup(page)
link = soup.findAll(lambda tag: tag.name == 'a' and tag.findParent('dt', 'thumbnail'))
for anchor in link.findAll('a', title = True):
print anchor['title']
最初的“链接”显示了一个很好的阵列,其中包含八个视频块。然后,我尝试使用上面的代码来检查并取出标签中的标题,这给了我以下错误:
for anchor in link.findAll('a', title=True):
AttributeError: 'ResultSet' object has no attribute 'findAll'
我究竟做错了什么?
问题答案:
link
是Tag
对象的集合,您需要对其进行迭代。例如:
for anchor in link:
print anchor['title']