C # code Implementation Baidu Search page page traversal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.