自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET數(shù)據(jù)采集的實(shí)現(xiàn)淺析

開發(fā) 后端
ASP.NET數(shù)據(jù)采集的實(shí)現(xiàn)向你介紹了如何通過程序?qū)崿F(xiàn)ASP.NET數(shù)據(jù)采集的功能,程序中使用了for循環(huán)來實(shí)現(xiàn),希望對(duì)你有所幫助。

這是我自己寫的一個(gè)ASP.NET數(shù)據(jù)采集程序,是采集一個(gè)人才網(wǎng)里面人才的信息,第一次寫B(tài)log,寫的不好不要見笑。

  1. //先按照下面的字段創(chuàng)建一個(gè)數(shù)據(jù)表  ,以便ASP.NET數(shù)據(jù)采集的歸納
  2.  
  3. 以下為引用的內(nèi)容:  
  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.         //學(xué)歷  
  23.         public static string XL = "";  
  24.         //婚姻狀況  
  25.         public static string HK = "";  
  26.         //所學(xué)專業(yè)  
  27.         public static string ZY = "";  
  28.         //工作經(jīng)驗(yàn)  
  29.         public static string GZJY = "";  
  30.         //在職單位  
  31.         public static string ZZDW = "";  
  32.         //在職職位  
  33.         public static string ZZZW = "";  
  34.         //工作經(jīng)歷  
  35.         public static string GZJL = "";  
  36.         //要求月薪  
  37.         public static string YX = "";  
  38.         //工作性質(zhì)  
  39.         public static string GZXZ = "";  
  40.         //求職意向  
  41.         public static string QZYX = "";  
  42.         //具體職務(wù)  
  43.         public static string JTZW = "";  
  44.         //期望工作地  
  45.         public static string QWGZD = "";  
  46.         //教育情況,語言水平,技術(shù)專長  
  47.         public static string QT = "";   
  48.         private void button1_Click(object sender, EventArgs e)  
  49.         {  
  50.             label1.Text = "正在采集數(shù)據(jù)……";  
  51.  
  52.      //遍歷數(shù)據(jù)的頁數(shù)   
  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數(shù)據(jù)采集之采集數(shù)據(jù)類
  63.         private void CJ(string Url)  
  64.         {  
  65.      //獲得頁面源文件(Html)  
  66.             string strWebContent = YM(Url);  
  67.  
  68.             //按照Html里面的標(biāo)簽  取出和數(shù)據(jù)有關(guān)的那段源碼  
  69.             int iBodyStart = strWebContent.IndexOf("﹤body", 0);  
  70.             int aaa = strWebContent.IndexOf("關(guān)鍵字:", 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.       //獲得詳細(xì)信息頁面的網(wǎng)址  
  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數(shù)據(jù)采集之采集詳細(xì)數(shù)據(jù)類  
  95.         private void Content(string URL)  
  96.         {  
  97.             try 
  98.             {  
  99.                 string strWebContent = YM(URL);  
  100.  
  101. //按照Html里面的標(biāo)簽 取出和數(shù)據(jù)有關(guān)的那段源碼  
  102.                 int iBodyStart = strWebContent.IndexOf("﹤body", 0);  
  103.                 int iTableStart = strWebContent.IndexOf("瀏覽次數(shù)", 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.         //學(xué)歷  
  126.                         XL = tr.GetElementsByTagName("tr")[3].GetElementsByTagName("td")[1].InnerText;  
  127.         //婚煙狀況  
  128.                         HK = tr.GetElementsByTagName("tr")[3].GetElementsByTagName("td")[3].InnerText;  
  129.         //所學(xué)專業(yè)  
  130.                         ZY = tr.GetElementsByTagName("tr")[5].GetElementsByTagName("td")[1].InnerText;  
  131.         //工作經(jīng)驗(yàn)  
  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.         //工作經(jīng)歷  
  138.                         GZJY = tr.GetElementsByTagName("tr")[7].GetElementsByTagName("td")[1].InnerText;  
  139.         //要求月薪  
  140.                         YX = tr.GetElementsByTagName("tr")[9].GetElementsByTagName("td")[1].InnerText;  
  141.         //工作性質(zhì)  
  142.                         GZXZ = tr.GetElementsByTagName("tr")[9].GetElementsByTagName("td")[3].InnerText;  
  143.         //求職意向  
  144.                         QZYX = tr.GetElementsByTagName("tr")[10].GetElementsByTagName("td")[1].InnerText;  
  145.         //具體職務(wù)  
  146.                         JTZW = tr.GetElementsByTagName("tr")[10].GetElementsByTagName("td")[3].InnerText;  
  147.         //期望工作地  
  148.                         QWGZD = tr.GetElementsByTagName("tr")[11].GetElementsByTagName("td")[1].InnerText;  
  149.         //教育情況,語言水平,技術(shù)專長  
  150.                         QT = tr.GetElementsByTagName("tr")[13].GetElementsByTagName("td")[1].InnerText;  
  151.  
  152.                         insert();  
  153.                     }  
  154.                     catch 
  155.                     { }  
  156.                 }  
  157.             }  
  158.             catch { }  
  159.         }  
  160.  
  161.       //將數(shù)據(jù)插入數(shù)據(jù)庫    
  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 人才信息 (姓名,年齡,性別,身高,政治面貌,民族,學(xué)歷,婚煙狀況,所學(xué)專業(yè),";  
  168.                 sql += "工作經(jīng)驗(yàn),在職單位,在職職位,工作經(jīng)歷,要求月薪,工作性質(zhì),求職意向,具體職務(wù),期望工作地,其他) 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.         //返回一個(gè)HtmlElementCollection,然后進(jìn)行查詢內(nèi)容  
  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返回一個(gè)htmldocument對(duì)象,表示對(duì)一個(gè)html文檔的操作  
  194. //htmldocument對(duì)象是在xmldocument基礎(chǔ)上建立的,具有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.         //獲得網(wǎng)址原代碼  
  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.     } 

這個(gè)ASP.NET數(shù)據(jù)采集程序?qū)懙牟皇翘?,是用for循環(huán)遍歷出來的,效率不是太高,可以使用多線程實(shí)現(xiàn)ASP.NET數(shù)據(jù)采集,那么希望對(duì)你了解ASP.NET數(shù)據(jù)采集有所幫助。

【編輯推薦】

  1. ASP.NET的五大數(shù)據(jù)控件淺談
  2. ASP.NET數(shù)據(jù)綁定控件比較淺析
  3. ASP.NET的GridView與DataGrid控件比較淺析
  4. ASP.NET數(shù)據(jù)緩存機(jī)制淺析
  5. ASP.NET數(shù)據(jù)緩存之?dāng)?shù)據(jù)緩存淺談
責(zé)任編輯:仲衡 來源: 中國站長站
相關(guān)推薦

2009-07-29 10:52:09

數(shù)據(jù)采集程序ASP.NET技巧

2009-08-13 17:52:27

C#數(shù)據(jù)采集

2009-07-28 10:01:16

ASP.NET Exc

2009-07-27 15:34:11

MembershipASP.NET

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-07-22 18:03:00

ASP.NET ASP

2009-11-06 09:23:41

ASP.NET高效分頁

2009-07-23 10:37:43

2009-08-05 16:59:55

ASP.NET組件設(shè)計(jì)

2009-07-24 10:53:51

ASP.NET實(shí)現(xiàn)靜態(tài)

2009-07-27 10:18:12

TypeResolveASP.NET

2009-08-03 18:35:51

ASP.NET數(shù)據(jù)緩存

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET優(yōu)點(diǎn)

2009-07-31 18:12:58

ASP.NET數(shù)據(jù)綁定

2009-07-28 17:36:21

ASP.NET數(shù)據(jù)庫連

2009-08-04 14:56:34

ASP.NET數(shù)據(jù)類型

2009-07-29 14:12:45

ASP.NET tra

2009-08-03 13:38:18

ASP.NET編程模型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)