usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingmshtml;usingSystem.Threading;usingmshtml;namespacewindowsformsapplication3{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } /// <summary> ///open a Web page in a control/// </summary> Public voidBaidu () {stringURL ="http://www.baidu.com/s?wd="+TextBox1.Text; WebBrowser1.Navigate (URL. Trim ()); } Private voidButton1_Click (Objectsender, EventArgs e) {Baidu (); } /// <summary> ///opening a new page does not jump to another browser/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidWebbrowser1_newwindow (Objectsender, CancelEventArgs e) {E.cancel=true; WebBrowser1.Navigate (Webbrowser1.statustext); } /// <summary> ///get all nodes of Web page, traverse all nodes, if tagged text value is "next page", simulate click, go to Next page/// </summary> Public voidBianli () {IHTMLDocument2 doc= WebBrowser1.Document.DomDocument asIHTMLDocument2; foreach(IHTMLElement eleinchdoc.all) {if(Ele.innertext = ="Next page >") {Ele.click (); Break; } } } /// <summary> ///To interpret whether the Web page is loading completed/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidWebbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) { if(e.url==webBrowser1.Document.Url) {Bianliwangye (); } } Private voidButton2_Click (Objectsender, EventArgs e) { //Bianli ();Bianliwangye (); } /// <summary> ///enumeration gets all URLs of Baidu search page/// </summary> Public voidBianliwangye () {IHTMLDocument2 document= (IHTMLDocument2) webBrowser1.Document.DomDocument;//Get Source codeIHTMLElementCollection HC = (ihtmlelementcollection) document.all;//Get all Tags//MessageBox.Show (HC. ToString ()); foreach(IHTMLElement HinchHc//Traverse Tags { if(h.classname=="g"|| h.classname=="C-showurl")//classname interpretation by label { stringA= h.innerhtml;//get label text content if(A.contains (" in")) { intb = A.indexof (" in"); stringA1 = A.substring (0, b);//Intercept URLsMessageBox.Show (A1); }}} Bianli ();//when enumerating to the last URL of the current page, simulate click to go to the next page}}} copy code
C # code Implementation Baidu Search page page traversal