Python 使用BeautifulSoup删除元素中的选定标记

Python 使用BeautifulSoup删除元素中的选定标记,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,在一个页面中,我们有几个h1。在第一个h1中,我想删除classreadtime的标记。这是我的尝试。但是,不会删除该标记。我哪里做错了 h1s = main.select('h1') print("BEFORE: main.select('h1')", main.select('h1')) real_h1 = h1s[0] if real_h1.select('.read-time') is not None: real_h1.select('.read-time').clear

在一个页面中,我们有几个h1。在第一个h1中,我想删除class
readtime
的标记。这是我的尝试。但是,不会删除该标记。我哪里做错了

h1s = main.select('h1')

print("BEFORE: main.select('h1')", main.select('h1'))

real_h1 = h1s[0]

if real_h1.select('.read-time') is not None:
    real_h1.select('.read-time').clear()

print("AFTER: main.select('h1')", main.select('h1'))
日志

BEFORE:main.select('h1')[开始之前,请先阅读简介]
在:main.select('h1')[开始之前,先阅读简介]
使用decompose()删除

html='''<h1>Introduction<span class="read-time"><span class="minutes"></span> min read</span></h1>, <h1 id="before-you-begin">Before You Begin</h1>]'''
main=BeautifulSoup(html,'html.parser')
h1s = main.select('h1')

print("BEFORE: main.select('h1')", main.select('h1'))

real_h1 = h1s[0]

if real_h1.select('.read-time') is not None:
    real_h1.decompose()

print("AFTER: main.select('h1')", main.select('h1'))
html=''开始之前,请先阅读简介]''
main=BeautifulSoup(html,'html.parser')
h1s=main。选择('h1')
打印(“BEFORE:main.select('h1')”,main.select('h1'))
实际_h1=h1s[0]
如果real_h1.select('.read time')不是None:
real_h1.decompose()
打印(“在:main.select('h1')”之后),main.select('h1'))
输出:

BEFORE: main.select('h1') [<h1>Introduction<span class="read-time"><span class="minutes"></span> min read</span></h1>, <h1 id="before-you-begin">Before You Begin</h1>]
AFTER: main.select('h1') [<h1 id="before-you-begin">Before You Begin</h1>]
BEFORE:main.select('h1')[开始之前,请先阅读简介]
之后:main.select('h1')[开始之前]
.select()
返回一个列表。按照KunduK的建议,迭代列表并分解:

h1s = main.select('h1')
print("BEFORE: main.select('h1')", main.select('h1'))

real_h1 = h1s[0]

read_times = real_h1.select(".read-time")
for span in read_times:
    span.decompose()

print("AFTER: main.select('h1')", main.select('h1'))
BEFORE:main.select('h1')[开始之前,请先阅读简介]
之后:main.select('h1')[简介,开始之前]
h1s = main.select('h1')
print("BEFORE: main.select('h1')", main.select('h1'))

real_h1 = h1s[0]

read_times = real_h1.select(".read-time")
for span in read_times:
    span.decompose()

print("AFTER: main.select('h1')", main.select('h1'))
BEFORE: main.select('h1') [<h1>Introduction<span class="read-time"><span class="minutes"></span> min read</span></h1>, <h1 id="before-you-begin">Before You Begin</h1>]
AFTER: main.select('h1') [<h1>Introduction</h1>, <h1 id="before-you-begin">Before You Begin</h1>]