我可以将这两个块合并为一个:
编辑:除了像 Yacoby 在答案中所做的那样组合循环之外的任何其他方法。
for tag in soup.findAll(['script', 'form']):
tag.extract()
for tag in soup.findAll(id="footer"):
tag.extract()
我也可以将多个块合二为一:
for tag in soup.findAll(id="footer"):
tag.extract()
for tag in soup.findAll(id="content"):
tag.extract()
for tag in soup.findAll(id="links"):
tag.extract()
或者可能有一些 lambda 表达式,我可以在其中检查是否在数组中,或者任何其他更简单的方法。
另外我如何找到带有属性类的标签,因为类是保留关键字:
编辑:这部分由 soup.findAll(attrs={'class': 'noprint'}) 解决:
for tag in soup.findAll(class="noprint"):
tag.extract()