Reference content is as follows:
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
// Name
Public static string XM = "";
// Age
Public static string nl = "";
// Gender
Public static string XB = "";
// Height
Public static string SG = "";
// Political outlook
Public static string mm = "";
// Nationality
Public static string MZ = "";
// Education level
Public static string XL = "";
// Marital status
Public static string HK = "";
// Major
Public static string ZY = "";
// Work experience
Public static string GZJY = "";
// Employer
Public static string ZZDW = "";
// Position
Public static string ZZZW = "";
// Work experience
Public static string GZJL = "";
// Requires monthly salary
Public static string YX = "";
// Working nature
Public static string GZXZ = "";
// Intention to apply for a job
Public static string QZYX = "";
// Specific position
Public static string JTZW = "";
// Expected Place of Work
Public static string QWGZD = "";
// Education, language level, technical expertise
Public static string QT = "";
Private void button#click (object sender, EventArgs e)
{
Label1.Text = "collecting data ...... ";
// Traverse the data page
For (int I = 1; I <= 50; I ++)
{
CJ ("http://www.xcjob.cn/renli.asp? Pageno = "+ I );
}
Label1.Text = "Congratulations! collection is complete! ";
MessageBox. Show ("Congratulations! collection is complete! ");
}
// Collect data
Private void CJ (string Url)
{
// Obtain the page source file (Html)
String strWebContent = YM (Url );
// Extract the data-related source code based on the tags in the Html.
Int iBodyStart = strWebContent. IndexOf ("<body", 0 );
Int aaa = strWebContent. IndexOf ("Keyword:", iBodyStart );
Int iTableStart = strWebContent. IndexOf ("<table", aaa );
Int iTableEnd = strWebContent. IndexOf ("</table>", iTableStart );
String strWeb = strWebContent. Substring (iTableStart, iTableEnd-iTableStart );
// Generate HtmlDocument
HtmlElementCollection htmlTR = HtmlTR_Content (strWeb, "tr ");
Foreach (HtmlElement tr in htmlTR)
{
Try
{
// Name
XM = tr. GetElementsByTagName ("a") [0]. InnerText;
// Obtain the URL of the detailed information page
String a = tr. GetElementsByTagName ("a") [0]. GetAttribute ("href"). ToString ();
A = "http://www.xcjob.cn" + a. Substring (11 );
Content ();
}
Catch {}
}
}
// Collect detailed data
Private void Content (string URL)
{
Try
{
String strWebContent = YM (URL );
// Extract the data-related source code based on the tags in the Html.
Int iBodyStart = strWebContent. IndexOf ("<body", 0 );
Int iTableStart = strWebContent. IndexOf ("browsing Times", 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
{
// Age
Nl = tr. GetElementsByTagName ("tr") [1]. GetElementsByTagName ("td") [1]. InnerText;
// Gender
String XB_SG = tr. GetElementsByTagName ("tr") [1]. GetElementsByTagName ("td") [3]. InnerText;
XB = XB_SG.Substring (0, 1 );
// Height
SG = XB_SG.Substring (11 );
// Political outlook
Mm = tr. GetElementsByTagName ("tr") [2]. GetElementsByTagName ("td") [1]. InnerText;
// Nationality
MZ = tr. GetElementsByTagName ("tr") [2]. GetElementsByTagName ("td") [3]. InnerText;
// Education level
XL = tr. GetElementsByTagName ("tr") [3]. GetElementsByTagName ("td") [1]. InnerText;
// Marital status
HK = tr. GetElementsByTagName ("tr") [3]. GetElementsByTagName ("td") [3]. InnerText;
// Major
ZY = tr. GetElementsByTagName ("tr") [5]. GetElementsByTagName ("td") [1]. InnerText;
// Work experience
GZJY = tr. GetElementsByTagName ("tr") [5]. GetElementsByTagName ("td") [3]. InnerText;
// Employer
ZZDW = tr. GetElementsByTagName ("tr") [6]. GetElementsByTagName ("td") [1]. InnerText;
// Position
ZZZW = tr. GetElementsByTagName ("tr") [6]. GetElementsByTagName ("td") [3]. InnerText;
// Work experience
GZJY = tr. GetElementsByTagName ("tr") [7]. GetElementsByTagName ("td") [1]. InnerText;
// Requires monthly salary
YX = tr. GetElementsByTagName ("tr") [9]. GetElementsByTagName ("td") [1]. InnerText;
// Working nature
GZXZ = tr. GetElementsByTagName ("tr") [9]. GetElementsByTagName ("td") [3]. InnerText;
// Intention to apply for a job
QZYX = tr. GetElementsByTagName ("tr") [10]. GetElementsByTagName ("td") [1]. InnerText;
// Specific position
JTZW = tr. GetElementsByTagName ("tr") [10]. GetElementsByTagName ("td") [3]. InnerText;
// Expected Place of Work
QWGZD = tr. GetElementsByTagName ("tr") [11]. GetElementsByTagName ("td") [1]. InnerText;
// Education, language level, technical expertise
QT = tr. GetElementsByTagName ("tr") [13]. GetElementsByTagName ("td") [1]. InnerText;
Insert ();
}
Catch
{}
}
}
Catch {}
}
// Insert data into the database
Private void insert ()
{
Try
{
String str = "Provider = Microsoft. Jet. OleDb.4.0; Data Source = Data. mdb ";
String SQL = "insert into talent information (name, age, gender, height, political outlook, ethnicity, education, marital status, Major ,";
SQL + = "work experience, in-service organization, in-service position, work experience, requirements for monthly salary, job nature, job intention, specific job title, desired job location, other) 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 {}
}
// Return an HtmlElementCollection and query the content.
Private HtmlElementCollection HtmlTR_Content (string strWeb, string tj)
{
Try
{
// Generate HtmlDocument
WebBrowser webb = new WebBrowser ();
Webb. Navigate ("about: blank ");
// Window.doc ument returns an htmldocument object, indicating operations on an html document
// The htmldocument object is created on the basis of xmldocument and has all the method attributes of xmldocument
HtmlDocument htmldoc = webb. Document. OpenNew (true );
Htmldoc. Write (strWeb );
HtmlElementCollection htmlTR = htmldoc. GetElementsByTagName (tj );
Return htmlTR;
}
Catch {return null ;}
}
// Obtain the original URL code
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;
}
} |