C# 网页数据表格抓取数据

时间:2022-06-17 07:01:30

public List<string> datasearch()
{
List<string> list = new List<string>();

string url = @""+txtUrl.Text.Trim().ToString();

WebRequest request = WebRequest.Create(url); //请求url
WebResponse response = request.GetResponse(); //获取url数据
StreamReader reader = null;
switch (cboCode.SelectedItem.ToString())
{
case "UTF-8":
reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
break;
case "Default":
reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
break;
default:
reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
break;
}
string str = reader.ReadToEnd(); //将数据写入到textbox中

reader.Close();
reader.Dispose();
response.Close();
string strRegexR = @"(?<=<tr>)([\s\S]*?)(?=</tr>)"; //构造解析表格行数据的正则表达式
string strRegexD = @"(?<=<td[^>]*>[\s]*?)([\S]*)(?=[\s]*?</td>)"; //构造解析表格列数据的正则表达式
Regex regexR = new Regex(strRegexR);
MatchCollection mcR = regexR.Matches(str); //执行匹配
bool first = true;
foreach (Match mr in mcR)
{
Regex regexD = new Regex(strRegexD);
MatchCollection mcD = regexD.Matches(mr.Groups[0].ToString()); //执行匹配

string Mydata = "";
for (int i = 0;i < mcD.Count; i++)
{
Mydata += mcD[i].Value + " ";
}
list.Add(Mydata);
}

return list;
}

Form1.cs 文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;

namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cboCode.SelectedIndex = 0;
}

public List<string> datasearch()
{
List<string> list = new List<string>();

string url = @""+txtUrl.Text.Trim().ToString();

WebRequest request = WebRequest.Create(url); //请求url
WebResponse response = request.GetResponse(); //获取url数据
StreamReader reader = null;
switch (cboCode.SelectedItem.ToString())
{
case "UTF-8":
reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
break;
case "Default":
reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
break;
default:
reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
break;
}
string str = reader.ReadToEnd(); //将数据写入到textbox中

reader.Close();
reader.Dispose();
response.Close();
string strRegexR = @"(?<=<tr>)([\s\S]*?)(?=</tr>)"; //构造解析表格数据的正则表达式
string strRegexD = @"(?<=<td[^>]*>[\s]*?)([\S]*)(?=[\s]*?</td>)";
Regex regexR = new Regex(strRegexR);
MatchCollection mcR = regexR.Matches(str); //执行匹配
bool first = true;
foreach (Match mr in mcR)
{
Regex regexD = new Regex(strRegexD);
MatchCollection mcD = regexD.Matches(mr.Groups[0].ToString()); //执行匹配

string Mydata = "";
for (int i = 0;i < mcD.Count; i++)
{
Mydata += mcD[i].Value + " ";
}
list.Add(Mydata);
}

return list;
}
private void btnGet_Click(object sender, EventArgs e)
{
List<string> list = datasearch();

string str = "";
for (int i = 0; i < list.Count; i++)
{

str += list[i].ToString() + @"
";
}
txtResult.Text = str;
}

private void btnSave_Click(object sender, EventArgs e)
{
TXTHelper.TxtSaveByStr(@"" + txtSaveUrl.Text.Trim(), txtResult.Text);//C:\Users\Administrator\Desktop\Work File\网页表格抓取\1.txt
MessageBox.Show("存储成功!");
}

private void btnAdd_Click(object sender, EventArgs e)
{
TXTHelper.TxtAddByStr(@"" + txtSaveUrl.Text.Trim(), txtResult.Text);
MessageBox.Show("存储添加成功!");
}
}
}

TXTHelper.cs 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Specialized;

namespace Demo
{
public class TXTHelper
{
public static StringCollection Read_txt(string url) //读取TXT内容
{
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read);
/**/
///定义输出字符串
StringCollection collection = new StringCollection();

/**/
///初始化该字符串的长度为0


/**/
///为上面创建的文件流创建读取数据流
StreamReader read = new StreamReader(fs);

/**/
///设置当前流的起始位置为文件流的起始点
read.BaseStream.Seek(0, SeekOrigin.Begin);

/**/
///读取文件
while (read.Peek() > -1)
{
/**/
///取文件的一行内容并换行
///
string str = read.ReadLine();
collection.Add(str);
}