Beautiful Soup的findAll()在findall()结果上返回TypeError

2 投票
1 回答
14832 浏览
提问于 2025-04-16 05:29

你好,我刚开始学习Python和Beautiful Soup。我想从一个表格的特定部分提取文本。但是,发现用findAll找到的结果似乎不是我可以再次用findAll操作的BeautifulSoup类型。

select = soup.find('table',{'id':"tp_section_1"})
print "got the right table"
tissues = select.findAll('td',{"class":re.compile("tissue[10]")})
print "got the right cells, now I'd like to get just the text"
tissueText = tissues.findAll(text = True)

最后一行出错了,显示TypeError。我好像可以在find的结果上使用findAll,但在后续的结果上却不行。这是因为我需要逐个处理这些元素吗?

为了让你更清楚,tissues的内容在最后一行之前是这样的,我想提取像“Adrenal gland”这样的文本:

<td valign="top" height="15" class="tissue1" nowrap> <a class="tissue_link" href="normal_unit.php?antibody_id=20769&amp;mainannotation_id=2065466">Adrenal gland</a> </td>

1 个回答

3

是的,你需要逐个元素地处理。find 只会返回一个单独的元素,而 findAll 会返回一个列表,即使这个列表里只有一个项目。

撰写回答