读书人

怎么提取html中标签的属性值

发布时间: 2012-04-16 16:20:04 作者: rapoo

如何提取html中标签的属性值?
eg:下面是html中的部分,现在需要提取每个<LI><SPAN class="userPic"><A> </A></SPAN></LI>中的title和href的属性值,例如第一个为 : 郎咸平 langxianping ,请问如何操作?

HTML code
<LI class="userList t2">            <EM class="ico_num">1</EM>            <EM class="ico_state "></EM>            <SPAN class="userPic">              <A title="郎咸平(@郎咸平") href="/langxianping" rel="郎咸平(@郎咸平") 1333183314493="1" card="1" ctype="2">                <IMG alt="郎咸平(@郎咸平") src="http://t2.qlogo.cn/mbloghead/30cea038dd2a3fd8dae0/40">              </A>            </SPAN>            <SPAN class="userName">              <A title="郎咸平(@郎咸平") href="/langxianping" rel="郎咸平(@郎咸平") 1333183314493="51" card="1" ctype="2">郎咸平</A>            </SPAN>            <SPAN class="topData">17154863</SPAN>            <SPAN class="pint">郎咸平,美国宾西法尼亚大学沃顿商学院博士,现任香港中...</SPAN>            <DIV class="attentBox">              <INPUT style="DISPLAY: none" class="addAttention" value="收听"<i class=\'l\'></i>" type=button><A class='delAttention' href="http://t.qq.com/rank.php?id=5&p=1#">                取消<I class="l"></I>              </A><A class="delAttention" href="#">                取消<I class="\'l\'"></I>              </A>            </DIV>          </LI>          <LI class="userList t2">            <EM class="ico_num">2</EM>            <EM class="ico_state "></EM>            <SPAN class="userPic">              <A title="任志强(@任志强") href="/renzhiqiang" rel="任志强(@任志强") 1333183314493="2" card="1" ctype="2">                <IMG alt="任志强(@任志强") src="http://t3.qlogo.cn/mbloghead/43d038e3bcf59c7f09b8/40">              </A>            </SPAN>            <SPAN class="userName">              <A title="任志强(@任志强") href="/renzhiqiang" rel="任志强(@任志强") 1333183314493="52" card="1" ctype="2">任志强</A>            </SPAN>            <SPAN class="topData">7978901</SPAN>            <SPAN class="pint">任志强,北京市政协委员,现任华远地产股份有限公司董事...</SPAN>            <DIV class="attentBox">              <INPUT style="DISPLAY: none" class="addAttention" value="收听"<i class=\'l\'></i>" type=button><A class='delAttention' href="http://t.qq.com/rank.php?id=5&p=1#">                取消<I class="l"></I>              </A><A class="delAttention" href="#">                取消<I class="\'l\'"></I>              </A>            </DIV>          </LI>


[解决办法]

C# code
        static void Main(string[] args)        {            string htmlStr = @"你的HTML代码";                      Regex re = new Regex(@"(?is)<LI[^>]+>.*?<SPAN\s*class=""userPic"">\s*<a\s*title=""([^""]+)""\)\s*href=""([^""]+)"".*?</LI>", RegexOptions.None);            MatchCollection mc = re.Matches(htmlStr);            foreach (Match ma in mc)            {                Console.WriteLine(ma.Groups[1].Value);                Console.WriteLine(ma.Groups[2].Value);            }            Console.ReadLine();        }//结果:郎咸平(@郎咸平  /langxianping   //      任志强(@任志强  /renzhiqiang 


[解决办法]

探讨

对,txt保存的html没有上面的html有缩进
引用:
引用:

现在的问题是,我用的txt保存的从网页中提取的html,运行不出结果。但是我把该html保存为.xml文件在vs2010中格式化内容后,就可以得到结果,有什么好的方法可以直接运行原来没有格式化的html?
引用:
HTML code


<SPAN cla……

读书人网 >C#

热点推荐