我想转换成:
<span class = "foo">data-1</span>
<span class = "foo">data-2</span>
<span class = "foo">data-3</span>至
<span class = "foo"> data-1 data-2 data-3 </span>在Python中使用BeautifulSoup。此HTML部分存在于页面主体的多个区域中,因此我想最小化此部分并将其删除。实际上,mid类是和em类在一起的,所以最初是分开的。
发布于 2020-03-12 16:47:10
改编自this answer,展示了如何将其用于span标记:
span_tags = container.find_all('span')
# combine all the text from b tags
text = ''.join(span.get_text(strip=True) for span in span_tags)
# here you choose a tag you want to preserve and update its text
span_main = span_tags[0] # you can target it however you want, I just take the first one from the list
span_main.span.string = text # replace the text
for tag in span_tags:
if tag is not span_main:
tag.decompose()https://stackoverflow.com/questions/60649387
复制相似问题