【问题标题】:Removing CSS from text using regex in python 3在 python 3 中使用正则表达式从文本中删除 CSS
【发布时间】:2016-06-21 11:59:34
【问题描述】:

我有一个类似的字符串:

"<p>
<style type=""text/css"">
P { margin-bottom: 0.08in; direction: ltr; widows: 2; orphans: 2; }A:link { color: rgb(0, 0, 255); }    </style>
</p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
    <font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span   style=""font-size: 12px; background-color: rgb(238, 238, 238);"">blablabla. </span></font></p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span style=""font-size: 12px; background-color: rgb(238, 238, 238);"">tjatjatja</span></font><span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">tjetjetje</span><span style=""font-size: 12px; font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; background-color: rgb(238, 238, 238);"">.</span></p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">huehuehue</span></p>
"

我想删除第一个样式标签及其内容。我有一个像这样的正则表达式:

([\s\S]*)<style type=""text\/css"">[\s\S]+<\/style>([\s\S]*)

它只匹配第一个样式标签,但是当我尝试在 python 中删除它时:

re.sub(r'([\s\S]*)<style type=""text/css"">[\s\S]*</style>([\s\S]*)', r'\1\2', cell_text, flags=re.M)

它不起作用。我认为这要么与组有关,要么与多行字符串有关。有什么想法吗?

【问题讨论】:

  • 您至少必须使[\s\S]* 非贪婪([\s\S]*?),以防更多style 标签是可能的。
  • 而且...我不是 python 专家,但你的正则表达式有单引号 - 为什么 2 " 在里面?我猜这个字符串有 2,因为这就是你在 python 中转义引号的方式,但在单引号字符串中不应该是必需的,或者......?
  • 不确定示例数据为何包含引号。为了解决这个问题,我对包含正则表达式的原始字符串使用了单引号。

标签: regex python-3.x web-scraping


【解决方案1】:

改用解析器:

from bs4 import BeautifulSoup

string = """
<p>
<style type=""text/css"">
P { margin-bottom: 0.08in; direction: ltr; widows: 2; orphans: 2; }A:link { color: rgb(0, 0, 255); }    </style>
</p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
    <font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span   style=""font-size: 12px; background-color: rgb(238, 238, 238);"">blablabla. </span></font></p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span style=""font-size: 12px; background-color: rgb(238, 238, 238);"">tjatjatja</span></font><span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">tjetjetje</span><span style=""font-size: 12px; font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; background-color: rgb(238, 238, 238);"">.</span></p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">huehuehue</span></p>
"""

soup = BeautifulSoup(string)
[s.extract() for s in soup('style')]
print soup

【讨论】:

  • 我正在考虑这样做,因为已经导入了 Beautifulsoup。您的解决方案非常有效!谢谢!
  • @tjarles:很高兴为您提供帮助 :)
  • @Jan:我认为反对票与this one I got 相同。有些人不喜欢设计。忽略这实际上对特定情况下的人们有所帮助的事实。
【解决方案2】:

要使用正则表达式删除 CSS,请使用以下正则表达式代码:

(?s)<style>(.*?)<\/style>

要在 Python 中使用“re”库进行替换,请执行以下操作:

regex = '(?s)<style>(.*?)<\/style>'
pattern = re.compile(regex)
re.sub(pattern, whatYouWantToReplaceItWith, stringToReplace)

以下是在 Python 中使用“re”库的教程: http://www.tutorialspoint.com/python/python_reg_expressions.htm

【讨论】:

  • 链接没有使用我示例中的字符串。如果您使用该字符串和正则表达式示例并输入它,则它与第一个标签匹配。这是我不确定的替代部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多