【发布时间】:2021-03-03 00:47:19
【问题描述】:
我有一个如下所示的字符串:
<b><!--
</b>if (window!= top)
top.location.href=location.href
<b>// -->
</b>
15 Minutes
EMIL (V.O.)
Just do what I do. Say the same thing I
say. Don't open your mouth.
我只想要从“15 分钟”开始的字符串,并且基于对 SO 上另一个问题的回答,我尝试像这样使用正则表达式:
def cleanhtml(raw_text):
cleanr = re.compile('<.*?>.*?')
cleantext = re.sub(cleanr, '', raw_text)
return cleantext
但这不会清除字符串的"if (window!= top) top.location.href=location.href" 部分。那我应该为正则表达式使用什么?
P.S.:我没有 HTML 文件。原始数据文件已经是.txt 形式。
【问题讨论】:
-
HTML 解析很难。使用第三方库,例如
bleach。 -
见stackoverflow.com/questions/11229831/…。那里的答案更详细。
-
一般来说,用正则表达式解析 HTML 被认为是一个坏主意(甚至可能是不可能的)。另一个流行的第三方库名为Beautiful Soup。