BeautifulSoup分解元素的多个类

问题描述

我想删除某些span元素,总共约10个单独的类。我可以重复10次代码来完成乏味的工作,但这不是正确的学习方法。

这就是我要简化的工作:

for span in table_tag.find_all("span",{'class':'class1'}):
    span.decompose()
for span in table_tag.find_all("span",{'class':'class2'}):
    span.decompose()
for span in table_tag.find_all("span",{'class':'class3'}):
    span.decompose()

这是我尝试过的,但没有删除任何东西:

remove = table_tag.find_all('class1','class2','class3')
for span in table_tag.find_all("span",{'class': remove}):
    span.decompose()

BeautifulSoup文档对如何在一个命令中分解多个元素并没有帮助我。同样,当我在find_all命令中超过5个类时,我得到一个错误,即find_all接受1到6个位置参数,但给出了7个。显然我使用的命令错误,请只需要一些帮助。

谢谢。

解决方法

classes = ['class1','class2'....]
for i in classes:
    for span in table_tag.find_all("span",{'class': i}):
        span.decompose()
,

您可以将CSS选择器与逗号//ViewController.swift class ViewController: UIViewController { var latitude : Float = 0.0 SendGeocodeRequest(query: query_origin) { ltd in self.latitude = ltd } } 一起使用:

,

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...