Python .decompose()之后未删除标记
我想删除强标记中的文本,所以我这样做了:Python .decompose()之后未删除标记,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我想删除强标记中的文本,所以我这样做了: for strong in soup.find('strong'): strong.decompose() 但当我打开文件时,最后一个文件没有被删除: 您使用的是find(),它只返回找到的第一个匹配项。这就是为什么您可以看到第一个strong消失了,而第二个没有。改为使用find_all(),这将删除所有strong标记 for strong in soup.find_all('strong'): strong.decompose()
for strong in soup.find('strong'):
strong.decompose()
但当我打开文件时,最后一个文件没有被删除:
您使用的是find()
,它只返回找到的第一个匹配项。这就是为什么您可以看到第一个strong
消失了,而第二个没有。改为使用find_all()
,这将删除所有strong
标记
for strong in soup.find_all('strong'):
strong.decompose()
我想您需要
strong.unwrap()
?没有任何更改上一个没有再次删除