【问题标题】:Replace headers by bold in HTML with python [duplicate]用python在HTML中用粗体替换标题[重复]
【发布时间】:2021-10-22 10:30:33
【问题描述】:

我有一些这样的 HTML 文本:

'<H1 LANG="es-ES" CLASS="western" STYLE="text-indent: -0.5cm; line-height: 100%"><FONT FACE="Arial, sans-serif"><FONT SIZE=3>some_text_here</FONT></FONT></H1>'

在较大的 HTML 文本中。我想自动识别所有此类标题并将它们更改为简单的粗体文本:

'<B LANG="es-ES" CLASS="western" STYLE="text-indent: -0.5cm; line-height: 100%"><FONT FACE="Arial, sans-serif"><FONT SIZE=3>some_text_here</FONT></FONT></B>'

使用正则表达式不是最好的,因为有时标题开始和结束在不同的行上。

【问题讨论】:

  • 好的,非常感谢!

标签: python html html-parsing


【解决方案1】:

您可以使用BeautifulSoup,但一种简单的方法是使用以下形式的re.sub()

   html_content = re.sub("<H\d", "<B", html_content)
   html_content = re.sub("<\/H\d>", "</B>", html_content)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 2013-01-10
    • 2021-05-21
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多