这是我自己写的一个ASP.NET数据采集程序,是采集一个人才网里面人才的信息,第一次写Blog,写的不好不要见笑 。
- ,以便ASP.NET数据采集的归纳
-
- 以下为引用的内容:
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- public static string XM = "";
-
- public static string nl = "";
-
- public static string XB = "";
-
- public static string SG = "";
-
- public static string mm = "";
-
- public static string MZ = "";
-
- public static string XL = "";
-
- public static string HK = "";
-
- public static string ZY = "";
-
- public static string GZJY = "";
-
- public static string ZZDW = "";
-
- public static string ZZZW = "";
-
- public static string GZJL = "";
-
- public static string YX = "";
-
- public static string GZXZ = "";
-
- public static string QZYX = "";
-
- public static string JTZW = "";
-
- public static string QWGZD = "";
-
- public static string QT = "";
- private void button1_Click(object sender, EventArgs e)
- {
- label1.Text = "正在采集数据……";
-
-
- for (int i = 1; i ﹤=50; i++)
- {
- CJ("http://www.xcjob.cn/renli.asp?pageno=" + i);
- }
-
- label1.Text = "恭喜你采集完成!";
- MessageBox.Show("恭喜你采集完成!");
- }
-
-
- private void CJ(string Url)
- {
-
- string strWebContent = YM(Url);
-
-
- int iBodyStart = strWebContent.IndexOf("﹤body", 0);
- int aaa = strWebContent.IndexOf("关键字:", iBodyStart);
- int iTableStart = strWebContent.IndexOf("﹤table", aaa);
- int iTableEnd = strWebContent.IndexOf("﹤/table﹥", iTableStart);
- string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart);
-
-
- HtmlElementCollection htmlTR = HtmlTR_Content(strWeb, "tr");
-
- foreach (HtmlElement tr in htmlTR)
- {
- try
- {
-
- XM = tr.GetElementsByTagName("a")[0].InnerText;
-
- string a = tr.GetElementsByTagName("a")[0].GetAttribute("href").ToString();
- a = "http://www.xcjob.cn" + a.Substring(11);
-
- Content(a);
- }
- catch { }
- }
- }
-
-
- private void Content(string URL)
- {
- try
- {
- string strWebContent = YM(URL);
-
-
- int iBodyStart = strWebContent.IndexOf("﹤body", 0);
- int iTableStart = strWebContent.IndexOf("浏览次数", iBodyStart);
- int iTableEnd = strWebContent.IndexOf("﹤table", iTableStart);
- int dd = strWebContent.IndexOf("﹤/table﹥", iTableEnd);
- string strWeb = strWebContent.Substring(iTableEnd, dd - iTableEnd + 8);
-
- HtmlElementCollection htmlTR = HtmlTR_Content(strWeb, "table");
-
- foreach (HtmlElement tr in htmlTR)
- {
- try
- {
-
- nl = tr.GetElementsByTagName("tr")[1].GetElementsByTagName("td")[1].InnerText;
-
- string XB_SG = tr.GetElementsByTagName("tr")[1].GetElementsByTagName("td")[3].InnerText;
- XB = XB_SG.Substring(0, 1);
-
- SG = XB_SG.Substring(11);
-
- mm = tr.GetElementsByTagName("tr")[2].GetElementsByTagName("td")[1].InnerText;
-
- MZ = tr.GetElementsByTagName("tr")[2].GetElementsByTagName("td")[3].InnerText;
-
- XL = tr.GetElementsByTagName("tr")[3].GetElementsByTagName("td")[1].InnerText;
-
- HK = tr.GetElementsByTagName("tr")[3].GetElementsByTagName("td")[3].InnerText;
-
- ZY = tr.GetElementsByTagName("tr")[5].GetElementsByTagName("td")[1].InnerText;
-
- GZJY = tr.GetElementsByTagName("tr")[5].GetElementsByTagName("td")[3].InnerText;
-
- ZZDW = tr.GetElementsByTagName("tr")[6].GetElementsByTagName("td")[1].InnerText;
-
- ZZZW = tr.GetElementsByTagName("tr")[6].GetElementsByTagName("td")[3].InnerText;
-
- GZJY = tr.GetElementsByTagName("tr")[7].GetElementsByTagName("td")[1].InnerText;
-
- YX = tr.GetElementsByTagName("tr")[9].GetElementsByTagName("td")[1].InnerText;
-
- GZXZ = tr.GetElementsByTagName("tr")[9].GetElementsByTagName("td")[3].InnerText;
-
- QZYX = tr.GetElementsByTagName("tr")[10].GetElementsByTagName("td")[1].InnerText;
-
- JTZW = tr.GetElementsByTagName("tr")[10].GetElementsByTagName("td")[3].InnerText;
-
- QWGZD = tr.GetElementsByTagName("tr")[11].GetElementsByTagName("td")[1].InnerText;
-
- QT = tr.GetElementsByTagName("tr")[13].GetElementsByTagName("td")[1].InnerText;
-
- insert();
- }
- catch
- { }
- }
- }
- catch { }
- }
-
-
- private void insert()
- {
- try
- {
- string str = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Data.mdb";
- string sql = "insert into 人才信息 (姓名,年龄,性别,身高,政治面貌,民族,学历,婚烟状况,所学专业,";
- sql += "工作经验,在职单位,在职职位,工作经历,要求月薪,工作性质,求职意向,具体职务,期望工作地,其他) values ";
- sql += "(" + XM + "," + nl + ","
- + XB + "," + SG + "," + mm + "," + MZ + ","
- + XL + "," + HK + "," + ZY + "," + GZJY + ","
- + ZZDW + "," + ZZZW + ",";
- sql += "" + GZJY + "," + YX + ","
- + GZXZ + "," + QZYX + "," + JTZW + "," + QWGZD + "," + QT + ")";
-
- OleDbConnection con = new OleDbConnection(str);
- OleDbCommand com = new OleDbCommand(sql, con);
- con.Open();
- com.ExecuteNonQuery();
- con.Close();
- }
- catch { }
- }
-
-
- private HtmlElementCollection HtmlTR_Content(string strWeb, string tj)
- {
- try
- {
-
- WebBrowser webb = new WebBrowser();
- webb.Navigate("about:blank");
-
-
- HtmlDocument htmldoc = webb.Document.OpenNew(true);
- htmldoc.Write(strWeb);
- HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName(tj);
-
- return htmlTR;
- }
- catch { return null; }
- }
-
-
-
- private string YM(string Url)
- {
- string strResult = "";
-
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- request.Method = "GET";
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream streamReceive = response.GetResponseStream();
- Encoding encoding = Encoding.GetEncoding("GB2312");
- StreamReader streamReader = new StreamReader(streamReceive, encoding);
- strResult = streamReader.ReadToEnd();
- }
- catch { }
-
- return strResult;
- }
- }
这个ASP.NET数据采集程序写的不是太好,是用for循环遍历出来的,效率不是太高,可以使用多线程实现ASP.NET数据采集,那么希望对你了解ASP.NET数据采集有所帮助 。