C # language Input keywords, crawl all the URLs you want

Source: Internet
Author: User

            usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingmshtml;usingSystem.Collections;usingSystem.Threading;namespacetraverse Baidu Web page { Public Delegate voidbaidu111 ();  Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {Listview1.gridlines=true;         } thread thread; voidBaidu () {baidu111 (); }         voidbaidu111 () {if(webbrowser1.invokerequired) {baidu111 Dr=Newbaidu111 (baidu111);  This.             Invoke (DR); }             Else             {                 stringURL ="http://www.baidu.com/s?wd="+TextBox1.Text; WebBrowser1.Navigate (URL.             Trim ()); }         }        //Click Stop         Private voidButton2_Click (Objectsender, EventArgs e) {                      }        Private voidButton1_Click (Objectsender, EventArgs e) {        //System.Diagnostics.Process.Start ("http://www.baidu.com"); //webbrowser1.navigate ("http://www.baidu.com/");Thread =NewThread (NewThreadStart (Baidu)); Thread. IsBackground=true; Thread.                     Start ();        Baidu (); }        inti =0;  Public voidBianli () {IHTMLDocument2 doc= WebBrowser1.Document.DomDocument asIHTMLDocument2; foreach(IHTMLElement eleinchdoc.all) {if(Ele.innertext = ="Next page >") {Ele.click (); I++; if(i==3)                    {                         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(i==3)            {                return; }            if(E.url = =webBrowser1.Document.Url) {Bianliwangye (); }        }        /// <summary>        ///enumeration gets all URLs of Baidu search page/// </summary>        /// ArrayList All=NewArrayList (); stringA1;  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"); A1= A.substring (0, b);//Intercept URLsMessageBox.Show (A1); All.                    ADD (A1); }}} Bianli ();//when enumerating to the last URL of the current page, simulate click to go to the next page                 }        Private voidButton3_Click (Objectsender, EventArgs e) {LISTVIEW1.COLUMNS.ADD ("numbering"); LISTVIEW1.COLUMNS.ADD ("get to the URL", -);  for(inti =0; I < All. Count; i++) {ListView1.Items.Add ((i+1).                ToString ()); Listview1.items[i]. SubItems.Add (All[i].            ToString ()); }        }                }}

C # language Input keywords, crawl all the URLs you want

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.