我希望从一个常规的HTML页面中提取所有文本(无论是否显示)。
我想要 删除
- 任何HTML标签
- 任何JavaScript
- 任何CSS样式
是否有一个或多个正则表达式可以实现这一点?
我希望从一个常规的HTML页面中提取所有文本(无论是否显示)。
我想要 删除
是否有一个或多个正则表达式可以实现这一点?
移除 JavaScript 和 CSS:
<(script|style).*?</\1>
去除标签
<.*?>
使用正则表达式无法真正解析HTML。它太复杂了。正则表达式无法正确处理<![CDATA[
区段。此外,一些常见的HTML东西,如<text>
在浏览器中可以作为正确文本工作,但可能会使一个天真的正则表达式感到困惑。
使用适当的HTML解析器将使您更加愉快和成功。Python用户通常使用Beautiful Soup之类的工具来解析HTML并剥离标签和脚本。
另外,浏览器设计时容忍不规范的HTML。因此,您经常会发现自己尝试解析明显不正确但在浏览器中运行良好的HTML。
您可能能够使用正则表达式解析不良HTML。这只需要耐心和刻苦工作。但使用别人的解析器通常更简单。
我需要一个(在php中的)正则表达式解决方案,它可以比PHPSimpleDOM更快地返回纯文本。以下是我想出的解决方案:
function plaintext($html)
{
// remove comments and any content found in the the comment area (strip_tags only removes the actual tags).
$plaintext = preg_replace('#<!--.*?-->#s', '', $html);
// put a space between list items (strip_tags just removes the tags).
$plaintext = preg_replace('#</li>#', ' </li>', $plaintext);
// remove all script and style tags
$plaintext = preg_replace('#<(script|style)\b[^>]*>(.*?)</(script|style)>#is', "", $plaintext);
// remove br tags (missed by strip_tags)
$plaintext = preg_replace("#<br[^>]*?>#", " ", $plaintext);
// remove all remaining html
$plaintext = strip_tags($plaintext);
return $plaintext;
}
<li >
(带有额外的空格)。 - minion如果考虑使用正则表达式来完成这个任务,可能会让人感到困难。您是否考虑过使用XSLT?提取XHTML文档中所有文本节点的XPath表达式,不包括脚本和样式内容,如下:
//body//text()[not(ancestor::script)][not(ancestor::style)]
text = "<p>This is my> <strong>example</strong>HTML,<br /> containing tags</p>"
import re
" ".join([t.strip() for t in re.findall(r"<[^>]+>|[^<]+",text) if not '<' in t])
'This is my> example HTML, containing tags'
function strip_html_tags( $text )
{
$text = preg_replace(
array(
// Remove invisible content
'@<head[^>]*?>.*?</head>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<object[^>]*?.*?</object>@siu',
'@<embed[^>]*?.*?</embed>@siu',
'@<applet[^>]*?.*?</applet>@siu',
'@<noframes[^>]*?.*?</noframes>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
'@<noembed[^>]*?.*?</noembed>@siu',
// Add line breaks before & after blocks
'@<((br)|(hr))@iu',
'@</?((address)|(blockquote)|(center)|(del))@iu',
'@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
'@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
'@</?((table)|(th)|(td)|(caption))@iu',
'@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
'@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
'@</?((frameset)|(frame)|(iframe))@iu',
),
array(
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
"\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
"\n\$0", "\n\$0",
),
$text );
// Remove all remaining tags and comments and return.
return strip_tags( $text );
}
使用Perl语法来定义正则表达式,一个开始的方法可能是:
!<body.*?>(.*)</body>!smi
!<script.*?</script>!!smi
!<[^>]+/[ \t]*>!!smi
!</?([a-z]+).*?>!!smi
/<!--.*?-->//smi
你不能只使用C#中可用的WebBrowser控件吗?
System.Windows.Forms.WebBrowser wc = new System.Windows.Forms.WebBrowser();
wc.DocumentText = "<html><body>blah blah<b>foo</b></body></html>";
System.Windows.Forms.HtmlDocument h = wc.Document;
Console.WriteLine(h.Body.InnerText);
string decode = System.Web.HttpUtility.HtmlDecode(your_htmlfile.html);
Regex objRegExp = new Regex("<(.|\n)+?>");
string replace = objRegExp.Replace(g, "");
replace = replace.Replace(k, string.Empty);
replace.Trim("\t\r\n ".ToCharArray());
then take a label and do "label.text=replace;" see on label out put
.
string replace = objRegExp.Replace(decode, "");
- mahesh