【问题标题】:Using Python and Regex,How do you remove <sup> tags from html? [duplicate]使用 Python 和 Regex,如何从 html 中删除 <sup> 标签? [复制]
【发布时间】:2014-08-23 09:53:14
【问题描述】:

使用 python 正则表达式,我如何删除 html 中的所有 标签?标签有时具有样式,如下所示:

<sup style="vertical-align:top;line-height:120%;font-size:7pt">(1)</sup>

我想删除更大的 html 字符串中的 sup 标签之间的所有内容。

【问题讨论】:

  • 你的最终结果是什么?
  • OP 必读的正则表达式操作 HTML:stackoverflow.com/a/1732454/3001761
  • 我通过将 html 转换为字符串并使用以下内容解决了我的问题:re.sub(r'+','',string of html)

标签: python html regex


【解决方案1】:

我会改用 HTML 解析器 (why)。例如,BeautifulSoupunwrap() 可以处理您的美餐

Tag.unwrap() 与 wrap() 相反。它将标签替换为 该标签内的任何内容。这对剥离标记很有用。

from bs4 import BeautifulSoup

data = """
<div>
    <sup style="vertical-align:top;line-height:120%;font-size:7pt">(1)</sup>
</div>
"""

soup = BeautifulSoup(data)
for sup in soup.find_all('sup'):
    sup.unwrap()

print soup.prettify()

打印:

<div>
(1)
</div>

【讨论】:

  • 谢谢这更有效。我很感激。
猜你喜欢
  • 1970-01-01
  • 2014-10-08
  • 2011-11-12
  • 2020-07-19
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2011-07-20
相关资源
最近更新 更多