日期:2014-05-18  浏览次数:20888 次

如何提取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&amp;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&amp;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