BeautifulSoup 分解一个元素的多个 类
BeautifulSoup decompose multiple classes of an element
我想删除某些跨度元素,总共大约有 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 选择器 ,
:
for span in table_tag.select('.class1, .class2, .class3'):
span.decompose()
我想删除某些跨度元素,总共大约有 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 选择器 ,
:
for span in table_tag.select('.class1, .class2, .class3'):
span.decompose()