asp.net 如何查找html某一标记里面的内容呢? 如题,我在CS文件里面,用string str取出了某张html文件的源代码那么我想利用js的getElementsByTagName原理,才取出str的某些节点内容可以怎么实现呢用CS或者JS写都行 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 即然是在string里面,可以用string的indexoOf("<TagName进行查找,然后找到"/>";循环取出一段段的string来不就OK了 为什么不用正则表达式处理字符串得出HTML中的内容,如果用JS岂不是必须先输出到客户端才能得到值!你说的不具体 没法写! 我大概也能想到用正则,就是想问问有谁有通用的正则表达式什么的,或者例子,给我一个最好是写好的方法,我只要调用这个方法,输入str和想找的标记就好我大概说下吧,我想找那些页面有id属性的所有标记下的东西没有id的那些不管 我大概也能想到用正则,就是想问问有谁有通用的正则表达式什么的,或者例子,给我一个 最好是写好的方法,我只要调用这个方法,输入str和想找的标记就好 我大概说下吧,我想找那些页面有id属性的所有标记下的东西 没有id的那些不管 <body><div><div class="left"> <div class="spic"><img src="images/1_01.gif" /></div> <div> <ul> <li><strong>主 办:</strong><em>中国元素网</em></li> <li><strong>主 编:</strong><em>中国元素网</em></li> <li><strong>编 辑:</strong><em>中国元素网</em></li> <li><strong>设计总监:</strong><em>中国元素网</em></li> <li><strong>设 计:</strong><em>中国元素网<br />中国元素网</em></li> <li><strong>技术支持:</strong><em>中国元素网</em></li> </ul> <dl> <dt>联系方式:</dt> <dd>[内容合作] 0754-00000000</dd> <dd>[广告投放] 0754-00000000</dd> </dl> </div></div><div class="center"></div><div class="right"> <div class="directory"> <h1>目录</h1> <label>Directory</label> <em>Vol/01</em> </div> <div class="dc"> <dl> <dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd> <dd class="title"><a href="#">饮食新闻</a></dd> <dd class="des">介绍饮料界新闻 信息</dd> </dl> </div> <div class="dc"> <dl> <dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd> <dd class="title"><a href="#">饮食新闻</a></dd> <dd class="des">介绍饮料界新闻 信息</dd> </dl> </div> <div class="dc"> <dl> <dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd> <dd class="title"><a href="#">饮食新闻</a></dd> <dd class="des">介绍饮料界新闻 信息</dd> </dl> </div> <div class="dc" id="2"> <dl> <dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd> <dd class="title"><a href="#">饮食新闻</a></dd> <dd class="des">介绍饮料界新闻 信息</dd> </dl> </div> <div class="dc" id="1"> <dl> <dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd> <dd class="title"><a href="#">饮食新闻</a></dd> <dd class="des">介绍饮料界新闻 信息</dd> </dl> </div> </div></div></body></html>这就是代码,我想找那2个有id的内容 前几天收藏的几种方法------------C# code public class Reg { #region/// 过滤html,js,css代码 /// <summary> /// 过滤html,js,css代码 /// </summary> /// <param name="html">参数传入</param> /// <returns></returns> public static string CheckStr(string html) { System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); html = regex1.Replace(html, ""); //过滤<script></script>标记 html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 html = regex4.Replace(html, ""); //过滤iframe html = regex5.Replace(html, ""); //过滤frameset html = regex6.Replace(html, ""); //过滤frameset html = regex7.Replace(html, ""); //过滤frameset html = regex8.Replace(html, ""); //过滤frameset html = regex9.Replace(html, ""); html = html.Replace(" ", ""); html = html.Replace("</strong>", ""); html = html.Replace("<strong>", ""); return html; } #endregion #region /// 过滤p /p代码 /// <summary> /// 过滤p /p代码 /// </summary> /// <param name="html">参数传入</param> /// <returns></returns> public static string InputStr(string html) { html = html.Replace(@"\<img[^\>]+\>", ""); html = html.Replace(@"<p>", ""); html = html.Replace(@"</p>", ""); return html; } #endregion /// <summary> /// 截取字符串 /// </summary> /// <param name="str"></param> /// <param name="length"></param> /// <returns></returns> protected string GetLength(string str, int length) { string strR = str; if (str.Length > length) { strR = str.Substring(0, length) + "..."; } return strR; } }-----------------------------直接replace也行 using System.Text.RegularExpressions; string html = "test start <iframe src=''> </iframe> end"; html = Regex.Replace(html, @" <iframe.+? </iframe>", "", RegexOptions.IgnoreCase);//IgnoreCase即不限制大小写--------------------C# code ///<summary> ///替换html中的特殊字符 ///</summary> ///<param name="theString">需要进行替换的文本。</param> ///<returns>替换完的文本。</returns> public string HtmlEncode(string theString) { theString = theString.Replace(">", ">"); theString = theString.Replace("<", "<"); theString = theString.Replace(" ", " "); theString = theString.Replace(" ", " "); theString = theString.Replace("\"", """); theString = theString.Replace("\'", "'"); theString = theString.Replace("\n", "<br/> "); return theString; }------------------ 楼上的。你要想到一件事就是说div里面可以嵌套div比如<div id=1> <div></div></div> 我有一个想法,就是能不能把html换成xml,这样会不会容易一点 File.Create 创建文件后,需要释放吗? 求正则表达式 请问aspx中怎么写逻辑判断 【讨论、请教】开发WEB应用系统,其中的通知消息如何设计比较好 网站占用cpu过高 运行报错 求一个SQL函数的写法(MSSQL区人太少了,只好发到这里),非常紧急 无法打开登录所请求的数据库 "GDMS"。登录失败。 高手快请进啊,我有问题,急 人才网JQUERY三级菜单 请问:Remoting用来干什么的? 在VS2005后台代码中引用前台的javascript函数??
最好是写好的方法,我只要调用这个方法,输入str和想找的标记就好
我大概说下吧,我想找那些页面有id属性的所有标记下的东西
没有id的那些不管
最好是写好的方法,我只要调用这个方法,输入str和想找的标记就好
我大概说下吧,我想找那些页面有id属性的所有标记下的东西
没有id的那些不管
<body>
<div>
<div class="left">
<div class="spic"><img src="images/1_01.gif" /></div>
<div>
<ul>
<li><strong>主 办:</strong><em>中国元素网</em></li>
<li><strong>主 编:</strong><em>中国元素网</em></li>
<li><strong>编 辑:</strong><em>中国元素网</em></li>
<li><strong>设计总监:</strong><em>中国元素网</em></li>
<li><strong>设 计:</strong><em>中国元素网<br />中国元素网</em></li>
<li><strong>技术支持:</strong><em>中国元素网</em></li>
</ul>
<dl>
<dt>联系方式:</dt>
<dd>[内容合作] 0754-00000000</dd>
<dd>[广告投放] 0754-00000000</dd>
</dl>
</div>
</div>
<div class="center"></div>
<div class="right">
<div class="directory">
<h1>目录</h1>
<label>Directory</label>
<em>Vol/01</em>
</div>
<div class="dc">
<dl>
<dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd>
<dd class="title"><a href="#">饮食新闻</a></dd>
<dd class="des">介绍饮料界新闻 信息</dd>
</dl>
</div>
<div class="dc">
<dl>
<dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd>
<dd class="title"><a href="#">饮食新闻</a></dd>
<dd class="des">介绍饮料界新闻 信息</dd>
</dl>
</div>
<div class="dc">
<dl>
<dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd>
<dd class="title"><a href="#">饮食新闻</a></dd>
<dd class="des">介绍饮料界新闻 信息</dd>
</dl>
</div>
<div class="dc" id="2">
<dl>
<dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd>
<dd class="title"><a href="#">饮食新闻</a></dd>
<dd class="des">介绍饮料界新闻 信息</dd>
</dl>
</div>
<div class="dc" id="1">
<dl>
<dd class="pic"><a href="#"><img src="images/1_02.gif" /></a></dd>
<dd class="title"><a href="#">饮食新闻</a></dd>
<dd class="des">介绍饮料界新闻 信息</dd>
</dl>
</div>
</div>
</div>
</body>
</html>这就是代码,我想找那2个有id的内容
------------
C# code
public class Reg
{
#region/// 过滤html,js,css代码
/// <summary>
/// 过滤html,js,css代码
/// </summary>
/// <param name="html">参数传入</param>
/// <returns></returns>
public static string CheckStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //过滤<script></script>标记
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
html = regex4.Replace(html, ""); //过滤iframe
html = regex5.Replace(html, ""); //过滤frameset
html = regex6.Replace(html, ""); //过滤frameset
html = regex7.Replace(html, ""); //过滤frameset
html = regex8.Replace(html, ""); //过滤frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
#endregion
#region /// 过滤p /p代码
/// <summary>
/// 过滤p /p代码
/// </summary>
/// <param name="html">参数传入</param>
/// <returns></returns>
public static string InputStr(string html)
{
html = html.Replace(@"\<img[^\>]+\>", "");
html = html.Replace(@"<p>", "");
html = html.Replace(@"</p>", "");
return html;
}
#endregion
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="str"></param>
/// <param name="length"></param>
/// <returns></returns>
protected string GetLength(string str, int length)
{
string strR = str;
if (str.Length > length)
{
strR = str.Substring(0, length) + "...";
}
return strR;
} }
-----------------------------
直接replace也行 using System.Text.RegularExpressions; string html = "test start <iframe src=''> </iframe> end";
html = Regex.Replace(html, @" <iframe.+? </iframe>", "", RegexOptions.IgnoreCase);//IgnoreCase即不限制大小写
--------------------
C# code ///<summary>
///替换html中的特殊字符
///</summary>
///<param name="theString">需要进行替换的文本。</param>
///<returns>替换完的文本。</returns>
public string HtmlEncode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace("\"", """);
theString = theString.Replace("\'", "'");
theString = theString.Replace("\n", "<br/> ");
return theString;
}------------------
就是说
div里面可以嵌套div
比如
<div id=1>
<div></div>
</div>