如何查找 BeautifulSoup 对象中文本对象的数量

How to find the number of text objects in BeautifulSoup object

我正在使用 python 中的 BeautifulSoup 抓取维基百科页面,我想知道是否有人知道 HTML 对象中文本对象的数量。例如下面的代码让我得到以下 HTML:

soup.find_all(class_ = 'toctext')

<span class="toctext">Actors and actresses</span>, <span class="toctext">Archaeologists and anthropologists</span>, <span class="toctext">Architects</span>, <span class="toctext">Artists</span>, <span class="toctext">Broadcasters</span>, <span class="toctext">Businessmen</span>, <span class="toctext">Chefs</span>, <span class="toctext">Clergy</span>, <span class="toctext">Criminals</span>, <span class="toctext">Conspirators</span>, <span class="toctext">Economists</span>, <span class="toctext">Engineers</span>, <span class="toctext">Explorers</span>, <span class="toctext">Filmmakers</span>, <span class="toctext">Historians</span>, <span class="toctext">Humourists</span>, <span class="toctext">Inventors / engineers</span>, <span class="toctext">Journalists / newsreaders</span>, <span class="toctext">Military: soldiers/sailors/airmen</span>, <span class="toctext">Monarchs</span>, <span class="toctext">Musicians</span>, <span class="toctext">Philosophers</span>, <span class="toctext">Photographers</span>, <span class="toctext">Politicians</span>, <span class="toctext">Scientists</span>, <span class="toctext">Sportsmen and sportswomen</span>, <span class="toctext">Writers</span>, <span class="toctext">Other notables</span>, <span class="toctext">English expatriates</span>, <span class="toctext">References</span>, <span class="toctext">See also</span>

我可以通过 运行 获取第一个文本对象:

soup.find_all(class_ = 'toctext')[0].text

我的目标是获取所有文本对象并将其存储在一个列表中。我通过使用 for 循环来执行此操作,但是我不知道 html 块中有多少文本对象。如果我到达一个不存在的索引,我自然会遇到错误。有替代方案吗?

您可以使用 for...in 循环。

In [13]: [t.text for t in soup.find_all(class_ = 'toctext')]
Out[13]: 
['Actors and actresses',
 'Archaeologists and anthropologists',
 'Architects',
 'Artists',
 'Broadcasters',
 'Businessmen',
 'Chefs',
 'Clergy',
 'Criminals',
 'Conspirators',
 'Economists',
 'Engineers',
 'Explorers',
 'Filmmakers',
 'Historians',
 'Humourists',
 'Inventors / engineers',
 'Journalists / newsreaders',
 'Military: soldiers/sailors/airmen',
 'Monarchs',
 'Musicians',
 'Philosophers',
 'Photographers',
 'Politicians',
 'Scientists',
 'Sportsmen and sportswomen',
 'Writers',
 'Other notables',
 'English expatriates',
 'References',
 'See also']

试试下面的代码:

for txt in soup.find_all(class_ = 'toctext'):
    print(txt.text)