ASP.NET数据采集的实现浅析

这是我自己写的一个ASP.NET数据采集程序,是采集一个人才网里面人才的信息,第一次写Blog,写的不好不要见笑。

 
 
 
  1. //先按照下面的字段创建一个数据表  ,以便ASP.NET数据采集的归纳
  2.  
  3. 以下为引用的内容:  
  4. public partial class Form2 : Form  
  5.     {  
  6.         public Form2()  
  7.         {  
  8.             InitializeComponent();  
  9.         }  
  10.         //姓名  
  11.         public static string XM = "";  
  12.         //年龄  
  13.         public static string nl = "";  
  14.         //性别  
  15.         public static string XB = "";  
  16.         //身高  
  17.         public static string SG = "";  
  18.         //政治面貌  
  19.         public static string mm = "";  
  20.         //民族  
  21.         public static string MZ = "";  
  22.         //学历  
  23.         public static string XL = "";  
  24.         //婚姻状况  
  25.         public static string HK = "";  
  26.         //所学专业  
  27.         public static string ZY = "";  
  28.         //工作经验  
  29.         public static string GZJY = "";  
  30.         //在职单位  
  31.         public static string ZZDW = "";  
  32.         //在职职位  
  33.         public static string ZZZW = "";  
  34.         //工作经历  
  35.         public static string GZJL = "";  
  36.         //要求月薪  
  37.         public static string YX = "";  
  38.         //工作性质  
  39.         public static string GZXZ = "";  
  40.         //求职意向  
  41.         public static string QZYX = "";  
  42.         //具体职务  
  43.         public static string JTZW = "";  
  44.         //期望工作地  
  45.         public static string QWGZD = "";  
  46.         //教育情况,语言水平,技术专长  
  47.         public static string QT = "";   
  48.         private void button1_Click(object sender, EventArgs e)  
  49.         {  
  50.             label1.Text = "正在采集数据……";  
  51.  
  52.      //遍历数据的页数   
  53.             for (int i = 1; i ﹤=50; i++)  
  54.             {  
  55.                 CJ("http://www.xcjob.cn/renli.asp?pageno=" + i);  
  56.             }  
  57.  
  58.             label1.Text = "恭喜你采集完成!";  
  59.             MessageBox.Show("恭喜你采集完成!");  
  60.         }  
  61.  
  62.         //ASP.NET数据采集之采集数据类
  63.         private void CJ(string Url)  
  64.         {  
  65.      //获得页面源文件(Html)  
  66.             string strWebContent = YM(Url);  
  67.  
  68.             //按照Html里面的标签  取出和数据有关的那段源码  
  69.             int iBodyStart = strWebContent.IndexOf("﹤body", 0);  
  70.             int aaa = strWebContent.IndexOf("关键字:", iBodyStart);  
  71.             int iTableStart = strWebContent.IndexOf("﹤table", aaa);  
  72.             int iTableEnd = strWebContent.IndexOf("﹤/table﹥", iTableStart);  
  73.             string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart);  
  74.  
  75.             //生成HtmlDocument  
  76.             HtmlElementCollection htmlTR = HtmlTR_Content(strWeb, "tr");  
  77.  
  78.             foreach (HtmlElement tr in htmlTR)  
  79.             {  
  80.                 try 
  81.                 {  
  82.       //姓名  
  83.                     XM = tr.GetElementsByTagName("a")[0].InnerText;  
  84.       //获得详细信息页面的网址  
  85.                     string a = tr.GetElementsByTagName("a")[0].GetAttribute("href").ToString();  
  86.                     a = "http://www.xcjob.cn" + a.Substring(11);  
  87.  
  88.                     Content(a);  
  89.                 }  
  90.                 catch { }  
  91.             }  
  92.         }  
  93.  
  94.         //ASP.NET数据采集之采集详细数据类  
  95.         private void Content(string URL)  
  96.         {  
  97.             try 
  98.             {  
  99.                 string strWebContent = YM(URL);  
  100.  
  101. //按照Html里面的标签 取出和数据有关的那段源码  
  102.                 int iBodyStart = strWebContent.IndexOf("﹤body", 0);  
  103.                 int iTableStart = strWebContent.IndexOf("浏览次数", iBodyStart);  
  104.                 int iTableEnd = strWebContent.IndexOf("﹤table", iTableStart);  
  105.                 int dd = strWebContent.IndexOf("﹤/table﹥", iTableEnd);  
  106.                 string strWeb = strWebContent.Substring(iTableEnd, dd - iTableEnd + 8);  
  107.  
  108.                 HtmlElementCollection htmlTR = HtmlTR_Content(strWeb, "table");  
  109.  
  110.                 foreach (HtmlElement tr in htmlTR)  
  111.                 {  
  112.                     try 
  113.                     {  
  114.         //年龄  
  115.                         nl = tr.GetElementsByTagName("tr")[1].GetElementsByTagName("td")[1].InnerText;  
  116.         //性别  
  117.                         string XB_SG = tr.GetElementsByTagName("tr")[1].GetElementsByTagName("td")[3].InnerText;  
  118.                         XB = XB_SG.Substring(0, 1);  
  119.         //身高  
  120.                         SG = XB_SG.Substring(11);  
  121.         //政治面貌  
  122.                         mm = tr.GetElementsByTagName("tr")[2].GetElementsByTagName("td")[1].InnerText;  
  123.         //民族  
  124.                         MZ = tr.GetElementsByTagName("tr")[2].GetElementsByTagName("td")[3].InnerText;  
  125.         //学历  
  126.                         XL = tr.GetElementsByTagName("tr")[3].GetElementsByTagName("td")[1].InnerText;  
  127.         //婚烟状况  
  128.                         HK = tr.GetElementsByTagName("tr")[3].GetElementsByTagName("td")[3].InnerText;  
  129.         //所学专业  
  130.                         ZY = tr.GetElementsByTagName("tr")[5].GetElementsByTagName("td")[1].InnerText;  
  131.         //工作经验  
  132.                         GZJY = tr.GetElementsByTagName("tr")[5].GetElementsByTagName("td")[3].InnerText;  
  133.         //在职单位  
  134.                         ZZDW = tr.GetElementsByTagName("tr")[6].GetElementsByTagName("td")[1].InnerText;  
  135.         //在职职位  
  136.                         ZZZW = tr.GetElementsByTagName("tr")[6].GetElementsByTagName("td")[3].InnerText;  
  137.         //工作经历  
  138.                         GZJY = tr.GetElementsByTagName("tr")[7].GetElementsByTagName("td")[1].InnerText;  
  139.         //要求月薪  
  140.                         YX = tr.GetElementsByTagName("tr")[9].GetElementsByTagName("td")[1].InnerText;  
  141.         //工作性质  
  142.                         GZXZ = tr.GetElementsByTagName("tr")[9].GetElementsByTagName("td")[3].InnerText;  
  143.         //求职意向  
  144.                         QZYX = tr.GetElementsByTagName("tr")[10].GetElementsByTagName("td")[1].InnerText;  
  145.         //具体职务  
  146.                         JTZW = tr.GetElementsByTagName("tr")[10].GetElementsByTagName("td")[3].InnerText;  
  147.         //期望工作地  
  148.                         QWGZD = tr.GetElementsByTagName("tr")[11].GetElementsByTagName("td")[1].InnerText;  
  149.         //教育情况,语言水平,技术专长  
  150.                         QT = tr.GetElementsByTagName("tr")[13].GetElementsByTagName("td")[1].InnerText;  
  151.  
  152.                         insert();  
  153.                     }  
  154.                     catch 
  155.                     { }  
  156.                 }  
  157.             }  
  158.             catch { }  
  159.         }  
  160.  
  161.       //将数据插入数据库    
  162.       private void insert()  
  163.         {  
  164.             try 
  165.             {  
  166.                 string str = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Data.mdb";  
  167.                 string sql = "insert into 人才信息 (姓名,年龄,性别,身高,政治面貌,民族,学历,婚烟状况,所学专业,";  
  168.                 sql += "工作经验,在职单位,在职职位,工作经历,要求月薪,工作性质,求职意向,具体职务,期望工作地,其他) values ";  
  169.                 sql += "('" + XM + "'," + nl + ",'" 
  170. + XB + "','" + SG + "','" + mm + "','" + MZ + "','" 
  171. + XL + "','" + HK + "','" + ZY + "','" + GZJY + "','" 
  172. + ZZDW + "','" + ZZZW + "',";  
  173.                 sql += "'" + GZJY + "','" + YX + "','" 
  174. + GZXZ + "','" + QZYX + "','" + JTZW + "','" + QWGZD + "','" + QT + "')";  
  175.  
  176.                 OleDbConnection con = new OleDbConnection(str);  
  177.                 OleDbCommand com = new OleDbCommand(sql, con);  
  178.                 con.Open();  
  179.                 com.ExecuteNonQuery();  
  180.                 con.Close();  
  181.             }  
  182.      catch { }  
  183.         }  
  184.  
  185.         //返回一个HtmlElementCollection,然后进行查询内容  
  186.         private HtmlElementCollection HtmlTR_Content(string strWeb, string tj)  
  187.         {  
  188.             try 
  189.             {  
  190. //生成HtmlDocument  
  191.                 WebBrowser webb = new WebBrowser();  
  192.                 webb.Navigate("about:blank");  
  193. //window.document返回一个htmldocument对象,表示对一个html文档的操作  
  194. //htmldocument对象是在xmldocument基础上建立的,具有xmldocument的一切方法属性  
  195.                 HtmlDocument htmldoc = webb.Document.OpenNew(true);  
  196.                 htmldoc.Write(strWeb);  
  197.                 HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName(tj);  
  198.  
  199.                 return htmlTR;  
  200.             }  
  201.             catch { return null; }  
  202.         }  
  203.  
  204.  
  205.         //获得网址原代码  
  206.         private string YM(string Url)  
  207.         {  
  208.             string strResult = "";  
  209.  
  210.             try 
  211.             {  
  212.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);  
  213.                 request.Method = "GET";  
  214.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  215.                 Stream streamReceive = response.GetResponseStream();  
  216.                 Encoding encoding = Encoding.GetEncoding("GB2312");  
  217.                 StreamReader streamReader = new StreamReader(streamReceive, encoding);  
  218.                 strResult = streamReader.ReadToEnd();  
  219.             }  
  220.             catch { }  
  221.  
  222.             return strResult;  
  223.         }  
  224.     } 

这个ASP.NET数据采集程序写的不是太好,是用for循环遍历出来的,效率不是太高,可以使用多线程实现ASP.NET数据采集,那么希望对你了解ASP.NET数据采集有所帮助。

【编辑推荐】

  1. ASP.NET的五大数据控件浅谈
  2. ASP.NET数据绑定控件比较浅析
  3. ASP.NET的GridView与DataGrid控件比较浅析
  4. ASP.NET数据缓存机制浅析
  5. ASP.NET数据缓存之数据缓存浅谈

本文名称:ASP.NET数据采集的实现浅析
网站链接:http://www.hantingmc.com/qtweb/news31/416781.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联