用C#做一个网页数据采集工具
用C#做一个网页数据采集工具
最近做一个网站,该网站需要添加4000多 产品信息,如果用人工方法去别的网站copy那至少要花费半月时间才能完成,所以我个办法使用c#作出来了一个网页数据采集软件,通过这个软件一两天就完成了几千产品数据的录入,可见很多工作不是一味用人工去做,作为一个程序员,就是要让很多让那些经常做重复性的、繁琐的工作中的人解放出来。下面只是写了一些核心代码,而且采集必须要和对应网站相挂钩,
//提取产品列表页中产品最终页的网页
private void button1_Click(object sender, EventArgs e)
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
MessageBox.Show("网址和域名不能为空!", "信息提示", MessageBoxButtons.OK, rmation);
string Html = inc.GetHtml("");
//ArrayList al = inc.GetMatchesStr(Html, "&a[^&]*?&.*?&/a&");
ArrayList al = inc.GetMatchesStr(Html, @"href\s*=\s*(?:[\'\""\s](?&1&[^\""\']*)[\'\""])");//提取链接
" title="Replica Watches:"&Replica Watches Buy Full Quality Popular Luxury Watches at Amazing Price, Your One Stop Discount Swiss Watches StoreExclusive Replica Rolex Watches, Tag Heuer Watches Replica, Cartier Watches online Sale!
文章来源: 自己用CSharp做一个网页数据采集工l
StringBuilder sb = new StringBuilder();
foreach (object var in al)
string a = var.ToString().Replace("\"", "").Replace("'", "");
a = Regex.Replace(a, ", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
if (a.StartsWith("/"))
a = textBox2.Text.Trim() +
if (!a.StartsWith(//"))
a = //" +
sb.Append(a + "\r\n");
textBox5.Text = sb.ToString();//把提取到网址输出到一个textBox,每个链接占一行
MessageBox.Show("共提取" + al.Count.ToString() + "个链接", "信息提示", MessageBoxButtons.OK, rmation);
catch (Exception err)
MessageBox.Show("提取出错!原因:" + err.Message, "信息提示", MessageBoxButtons.OK, rmation);
//把采集的产品页面html代码进行字符串处理,提取需要的代码,最后保存到本地一个access数据库中,同时提取产品图片地址并自动现在图片到本地images文件夹下
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
//填充产品表
Database.ExecuteNonQuery("delete from Tb_Product");
DataTable dt2 = new DataTable();
OleDbConnection conn = new OleDbConnection(Database.ConnectionStrings);
OleDbDataAdapter da = new OleDbDataAdapter("select * from Tb_Product", conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.Fill(dt2);
dt2.Rows.Clear();
BackgroundWorker worker = (BackgroundWorker)//这个是做一个进度条
string[] Urls = textBox5.Text.Trim().ToLower().Replace("\r\n", ",").Split(',');
DataTable dt = new DataTable();
StringBuilder ErrorStr = new StringBuilder();
string html = "", ImageDir = AppDomain.CurrentDomain.BaseDirectory + "Images\\";
//循环每次采集网址
for (int i = 0; i & Urls.L i++)
if (!worker.CancellationPending)
if (Urls[i] == "")
html = inc.GetHtml(Urls[i]);//获取该url的html代码
DataRow NewRow = dt2.NewRow();
//产品名
string ProductName = html.Substring(html.IndexOf("&title&") + 7);
NewRow["ProductName"] = ProductName.Remove(ProductName.IndexOf("&/title&")).Trim();
//产品编号
NewRow["ModelId"] = NewRow["ProductName"].ToString().Substring(NewRow["ProductName"].ToString().IndexOf("Model:") + 6).Trim();
//产品介绍,这些都是根据不同网站的html做相应的修改
string Introduce = html.Substring(html.IndexOf("Product Details") + 26);
Introduce = Introduce.Remove(Introduce.IndexOf("&/table&") + 8).Trim()
NewRow["Introduce"] = I
自己用CSharp做一个网页数据采集工l
" title="Replica Watches:"&Replica Watches Buy Full Quality Popular Luxury Watches at Amazing Price, Your One Stop Discount Swiss Watches StoreExclusive Replica Rolex Watches, Tag Heuer Watches Replica, Cartier Watches online Sale!
文章来源: 自己用CSharp做一个网页数据采集工l
//下载图片
string ProductImage = html.Substring(html.IndexOf("align=center&&img") + 17);
ProductImage = textBox2.Text.Trim() + ProductImage.Substring(ProductImage.IndexOf("src=\"") + 5);
ProductImage = ProductImage.Remove(ProductImage.IndexOf("\""));
inc.DownFile(ProductImage, ImageDir + ProductImage.Substring(ProductImage.LastIndexOf("/") + 1));
catch (Exception)
ErrorStr.Append("下载图片失败,图片地址:" + ImageDir + ProductImage.Substring(ProductImage.LastIndexOf("/") + 1) + "\r\n");
dt2.Rows.Add(NewRow);
//Thread.Sleep(100);
worker.ReportProgress((i + 1) * 100 / Urls.Length, i);
toolStripStatusLabel1.Text = "处理进度:" + (i + 1).ToString() + "/" + Urls.Length.ToString();//进度条
catch (Exception err)
ErrorStr.Append("采集错误:" + err.Message + ";网址:" + Urls[i] + "\r\n");
da.Update(dt2);
DataBind(dt2);
ShowError(ErrorStr.ToString());
/// &summary&
/// ASPX页面生成静态Html页面
/// &/summary&
public static string GetHtml(string url)
StreamReader sr =
string str =
//读取远程路径
WebRequest request = WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
str = sr.ReadToEnd();
sr.Close();
&&&主编推荐
H3C认证Java认证Oracle认证
基础英语软考英语项目管理英语职场英语
.NETPowerBuilderWeb开发游戏开发Perl
二级模拟试题一级模拟试题一级考试经验四级考试资料
港口与航道工程建设工程法规及相关知识建设工程经济考试大纲矿业工程市政公用工程通信与广电工程
操作系统汇编语言计算机系统结构人工智能数据库系统微机与接口
软件测试软件外包系统分析与建模敏捷开发
法律法规历年试题软考英语网络管理员系统架构设计师信息系统监理师
高级通信工程师考试大纲设备环境综合能力
路由技术网络存储无线网络网络设备
CPMP考试prince2认证项目范围管理项目配置管理项目管理案例项目经理项目干系人管理
Powerpoint教程WPS教程
电子政务客户关系管理首席信息官办公自动化大数据
职称考试题目
就业指导签约违约职业测评
招生信息考研政治
网络安全安全设置工具使用手机安全
3DMax教程Flash教程CorelDraw教程Director教程
Dreamwaver教程HTML教程网站策划网站运营Frontpage教程
生物识别传感器物联网传输层物联网前沿技术物联网案例分析
互联网电信IT业界IT生活
Java核心技术J2ME教程
Linux系统管理Linux编程Linux安全AIX教程
Windows系统管理Windows教程Windows网络管理Windows故障
组织运营财务资本
视频播放文件压缩杀毒软件输入法微博
数据库开发Sybase数据库Informix数据库
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&&&湘教QS2-164&&增值电信业务经营许可证湘B2-newtrader01
〉〉要運行C#??的程序首先就得裝??.net framework
光這??要求就不行了吧——java不也得用VM吗?不也能做嵌入式吗?
newtrader01
〉〉C#這??做嵌入式???不行的吧——可以的。
您的举报已经提交成功,我们将尽快处理,谢谢!
大家还关注
(window.slotbydup=window.slotbydup || []).push({
id: '2081942',
container: s,
size: '1000,60',
display: 'inlay-fix'学完C#《设计模式》,老是要求用5种模式做一个实例。求高手帮助
[问题点数:20分,结帖人caozhy]
学完C#《设计模式》,老是要求用5种模式做一个实例。求高手帮助
[问题点数:20分,结帖人caozhy]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2012年3月 总版技术专家分月排行榜第一
2013年7月 荣获微软MVP称号
本帖子已过去太久远了,不再提供回复功能。}