利用正则表达式提取网页中Table内的数据

181 篇文章 2 订阅
14 篇文章 0 订阅

利用正则表达式提取网页中Table内的数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string pagedata = @"<table class=""tabContent"" style=""width:681px;"">
     <tr><td width=""60"" id=""n001"">周六010</td>
    <td width=""100"">英格兰甲级联赛</td>
    <td id=""t036""><a href='http://info.sporttery.cn/football/history/information.php?match_id=7844' target='_blank'>诺维奇<strong> VS </strong>利兹联</a></td>
    <td width=""100"">03-27 22:59</td>
    <td width=""50""><input type=""checkbox"" id=""m036001"" name=""m036001"" value=""49465653"" οnclick=""select_match('036001')""/>1.85</td><td width=""50""><input type=""checkbox"" id=""m036002"" name=""m036002"" value=""51465053"" οnclick=""select_match('036002')""/>3.25</td><td width=""50""><input type=""checkbox"" id=""m036003"" name=""m036003"" value=""51465148"" οnclick=""select_match('036003')""/>3.30</td>    <td width=""50""><input type=""checkbox"" id=""all036"" οnclick=""bao('036',3)""/></td></tr>
   </tr>   <tr><td width=""60"" id=""n037"">周六046</td>
    <td width=""100"">苏格兰超级联赛</td>
    <td id=""t037""><a href='http://info.sporttery.cn/football/history/information.php?match_id=7845' target='_blank'>阿伯丁<strong> VS </strong>圣米伦</a></td>
    <td width=""100"">03-27 22:59</td>
    <td width=""50""><input type=""checkbox"" id=""m037001"" name=""m037001"" value=""49465656"" οnclick=""select_match('037001')""/>1.88</td><td width=""50""><input type=""checkbox"" id=""m037002"" name=""m037002"" value=""51464948"" οnclick=""select_match('037002')""/>3.10</td><td width=""50""><input type=""checkbox"" id=""m037003"" name=""m037003"" value=""51465153"" οnclick=""select_match('037003')""/>3.35</td>    <td width=""50""><input type=""checkbox"" id=""all037"" οnclick=""bao('037',3)""/></td></tr>
   </tr>   <tr><td width=""60"" id=""n038"">周六047</td>
    <td width=""100"">苏格兰超级联赛</td>
    <td id=""t038""><a href='http://info.sporttery.cn/football/history/information.php?match_id=7846' target='_blank'>凯尔特人(-1)<strong> VS </strong>基马诺克</a></td>
    <td width=""100"">03-27 22:59</td>
    <td width=""50""><input type=""checkbox"" id=""m038001"" name=""m038001"" value=""49465253"" οnclick=""select_match('038001')""/>1.45</td><td width=""50""><input type=""checkbox"" id=""m038002"" name=""m038002"" value=""52464853"" οnclick=""select_match('038002')""/>4.05</td><td width=""50""><input type=""checkbox"" id=""m038003"" name=""m038003"" value=""52465448"" οnclick=""select_match('038003')""/>4.60</td>    <td width=""50""><input type=""checkbox"" id=""all038"" οnclick=""bao('038',3)""/></td></tr>
   </tr>
</table>
";

        string result="";

        Regex re = new Regex(@"<tr><td.*?>(?<text1>.*?)</td>\s+?<td.*?>(?<text2>.*?)</td>\s+?<td.*?><a.*?>(?<text3>.*?)<strong> VS </strong>(?<text4>.*?)</a></td>\s+?<td.*?>(?<text5>.*?)</td>\s+?<td .*?><input .*?/>(?<text6>.*?)</td><td .*?><input .*?/>(?<text7>.*?)</td><td .*?><input .*?/>(?<text8>.*?)</td>"); ----*? 或+?表示非贪婪模式;\s+?决定了可以换行;使用RegexBuddy工具验证


        int i = 0;

        for (Match m = re.Match(pagedata); m.Success && i < 10; m = m.NextMatch())
        {

            result += m.Groups["text1"].Value.Trim() + "|";
            result += m.Groups["text2"].Value.Trim() + "|";
            result += m.Groups["text3"].Value.Trim() + "|";
            result += m.Groups["text4"].Value.Trim() + "|";
            result += m.Groups["text5"].Value.Trim() + "|";
            result += m.Groups["text6"].Value.Trim() + "|";
            result += m.Groups["text7"].Value.Trim() + "|";
            result += m.Groups["text8"].Value.Trim() + "|";
            result += "<br>";

            i++;

        }

        Response.Write(result);

    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值