使用Python BeautifulSoup识别highlight类span文本并保留顺序
问题:用BeautifulSoup保留段落文本顺序并识别高亮片段
需求是清理HTML文本时,识别出class='highlight'的span元素内的文本片段,同时严格保留这些文本在段落中的原始显示顺序。之前尝试用highlight_spans = soup.find_all('span', class_='highlight')提取高亮文本,但无法兼顾非高亮文本的顺序。
解决方案
核心思路是遍历目标段落标签下的所有直接子节点,区分每个节点是高亮span还是普通文本节点,分别记录文本内容和高亮标记,最后整理成DataFrame:
from bs4 import BeautifulSoup import pandas as pd original_string = """<div class="image-container half-saturation half-opaque" \ style="cursor: pointer;"><img src="../stim/microphone.png" style="width: 40px; height: 40px;">\ </div><p class="full-opaque">\ <span class="highlight">Easy to cultivate, sunflowers are a popular choice for gardeners of all skill levels</span>. \ Their large, <span class="highlight">cheerful blooms</span>\ bring a touch of summer to any outdoor space, creating a delightful atmosphere. \ Whether you're enjoying their beauty in a garden or using them to add a splash of color to your living space, \ sunflowers are a symbol of positivity and radiance, making them a beloved part of nature's tapestry.</p>""" # Parse the HTML content soup = BeautifulSoup(original_string, 'html.parser') # 获取目标p标签 target_p = soup.find('p', class_='full-opaque') # 初始化数据列表 text_order = [] text_content = [] highlight_flags = [] # 遍历p标签下的所有子节点 for idx, node in enumerate(target_p.contents): # 处理span高亮节点 if node.name == 'span' and 'highlight' in node.get('class', []): text_order.append(idx) text_content.append(node.get_text(strip=False)) highlight_flags.append(True) # 处理普通文本节点(排除空文本) elif node.string and node.string.strip(): text_order.append(idx) text_content.append(node.string.strip()) highlight_flags.append(False) # 整理成DataFrame data = { 'text_order': text_order, 'text': text_content, 'highlight': highlight_flags } df = pd.DataFrame(data) print(df)
输出结果
运行代码后会得到符合需求的DataFrame:
text_order text highlight 0 0 Easy to cultivate, sunflowers are a popular choice for gardeners... True 1 2 Their large, False 2 3 cheerful blooms True 3 4 bring a touch of summer to any outdoor space, creating a delig... False
内容的提问来源于stack exchange,提问作者psychcoder
相关产品推荐
相关产品推荐

