设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2734|回复: 1
打印 上一主题 下一主题

练习用正则表达式将网页table转换到DataGridView

[复制链接]
跳转到指定楼层
1#
发表于 2014-4-17 00:26:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 tianping 于 2014-4-17 00:33 编辑

小辰同学要求将一个网页的表格数据抓取下来,
故做此练习。请看原网页及转换结果

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2014-4-17 00:27:25 | 只看该作者
本帖最后由 tianping 于 2014-4-17 00:28 编辑

原代码
包含一小段抓取网页html代码(未包含登录)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Text.RegularExpressions;
  11. using System.IO;
  12. using System.Net;

  13. namespace NetCapture
  14. {
  15.     public partial class Form2 : Form
  16.     {
  17.         public Form2()
  18.         {
  19.             InitializeComponent();
  20.         }

  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             this.dataGridView1.Columns.Clear();
  24.             //定义表、表头、表体、表头单元格、数据行、数据单元格正则表达式
  25.             Regex tableR =new Regex( @"<table[\s\S]*?</table>",RegexOptions.Multiline);
  26.             Regex theadR = new Regex(@"<thead.*?</thead>");
  27.             Regex headCellR = new Regex(@"(?<=<th.*?>)[^>]*?(?=</th>)");
  28.             Regex rowR = new Regex(@"<tr.*?</tr>");
  29.             Regex cellR = new Regex(@"(?<=<td.*?>)[^>]*?(?=</td>)");
  30.             //定义变量
  31.             int tableID = 0;
  32.             int rowID = 0;
  33.             int columnNum = 0;
  34.             Dictionary<int,string> tableStrs = new Dictionary<int,string>();
  35.             Dictionary<int, string> headStrs = new Dictionary<int, string>();  
  36.             Dictionary<int,string> rowStrs = new Dictionary<int, string>();
  37.             Dictionary<int, string> cellStrs = new Dictionary<int, string>();

  38.             string htmlStr = Regex.Replace(this.richTextBox1.Text,@"(\s)|(</a>)|(</font>)",""); //去掉所有的</a>和空格
  39.             MatchCollection tableMatchs = tableR.Matches(htmlStr);
  40.             if (tableMatchs.Count<1)return;
  41.             // 取得匹配表
  42.             foreach (Match s in tableMatchs)            
  43.                 tableStrs[tableID++]=s.Value;
  44.             //处理第一个表,分为行
  45.             MatchCollection rowMatchs = rowR.Matches(tableStrs[0]);
  46.             foreach (Match s in rowMatchs)
  47.                 rowStrs[rowID++] = s.Value;
  48.             //取表头
  49.             MatchCollection columnMatchs = headCellR.Matches(rowStrs[0]);
  50.             //初始datagridview列
  51.             columnNum = columnMatchs.Count;
  52.             DataGridViewTextBoxColumn col;
  53.             for (int i = 1; i <columnNum; i++)
  54.             {
  55.                 col = new DataGridViewTextBoxColumn();
  56.                 col.Name ="col"+ i.ToString();
  57.                 col.HeaderText =columnMatchs[i].Value;
  58.                 this.dataGridView1.Columns.Add(col);               
  59.             }

  60.             //处理每数据行成单元格
  61.             for (int i = 1; i < rowStrs.Count; i++)
  62.             {
  63.                 MatchCollection cellMatchs=cellR.Matches(rowStrs[i]);
  64.                 int columnID = 0;
  65.                 int index = this.dataGridView1.Rows.Add();
  66.                 for (int j = 1; j < columnNum; j++)
  67.                 {
  68.                     this.dataGridView1.Rows[index].Cells[columnID++].Value = cellMatchs[j].Value;
  69.                 }   
  70.                                  
  71.             }
  72.         }
  73.                
  74.         private void button2_Click(object sender, EventArgs e)
  75.         {

  76.             WebRequest request = WebRequest.Create(textBox2.Text);
  77.             WebResponse response = request.GetResponse();
  78.             StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
  79.             this.richTextBox1.Text = reader.ReadToEnd();
  80.             reader.Close();
  81.             reader.Dispose();
  82.             response.Close();
  83.         }
  84.     }
  85. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 16:07 , Processed in 0.077462 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表