C # crawls and exports all hyperlink methods in a Web page

Source: Internet
Author: User

 Public classApp {//gets the HTML code for the specified Web page         Public Static stringGetpagesource (stringURL) {URI Uri=NewUri (URL); HttpWebRequest Hwreq=(HttpWebRequest) webrequest.create (URI); HttpWebResponse Hwres=(HttpWebResponse) hwreq.getresponse (); Hwreq.method="Get"; Hwreq.keepalive=false; StreamReader Reader=NewStreamReader (Hwres.getresponsestream (), System.Text.Encoding.GetEncoding ("GB2312")); returnReader.        ReadToEnd (); }        //extracting URLs from your HTML code         Public StaticArrayList Gethyperlinks (stringHtmlcode) {ArrayList Al=NewArrayList (); stringStrregex =@"http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)?"; Regex R=NewRegex (Strregex, regexoptions.ignorecase); MatchCollection m=r.matches (Htmlcode);  for(inti =0; I <= M.count-1; i++)            {                BOOLRep =false; stringStrnew =M[i].                ToString (); //Filter for duplicate URLs                foreach(stringStrinchal) {                    if(Strnew = =str) {Rep=true;  Break; }                }                if(!Rep) al.            ADD (strnew); } al.            Sort (); returnal; }        //write the URL to an XML file         Public Static voidWritetoxml (stringstrURL, ArrayList alhyperlinks) {XmlTextWriter writer=NewXmlTextWriter ("Hyperlinks.xml", Encoding.UTF8); Writer. Formatting=formatting.indented; Writer. WriteStartDocument (false); Writer. Writedoctype ("HyperLinks",NULL,"URLS.DTD",NULL); Writer. WriteComment ("Extracted from"+ strURL +"of Hyperlinks"); Writer. WriteStartElement ("HyperLinks"); Writer. WriteStartElement ("HyperLinks",NULL); Writer. WriteAttributeString ("DateTime", DateTime.Now.ToString ()); foreach(stringStrinchalhyperlinks) {                stringtitle =GetDomain (str); stringBODY =str; Writer. WriteElementString (title,NULL, body); } writer.            WriteEndElement (); Writer.            WriteEndElement (); Writer.            Flush (); Writer.        Close (); }        //get the domain name suffix of the URL        Static stringGetDomain (stringstrURL) {            stringRetVal; stringStrregex =@"(\.com/|\.net/|\.cn/|\.org/|\.gov/)"; Regex R=NewRegex (Strregex, regexoptions.ignorecase); Match m=R.match (strURL); RetVal=m.tostring (); Strregex=@"\.| /$"; RetVal= Regex.Replace (RetVal, Strregex,"").            ToString (); if(RetVal = ="") RetVal=" Other"; returnRetVal; }    }

Private voidBtnkaishi_click (Objectsender, EventArgs e) {            stringStrcode;            ArrayList allinks; if(Txtapi. Text = ="") {MessageBox.Show ("Please enter the URL"); return; }            stringstrURL =Txtapi. Text.tostring ().            Trim (); if(Strurl.substring (0,7) !=@"/ http") {strURL=@"/ http"+strURL; } MessageBox.Show ("getting page code, please later ..."); Strcode=app.            Getpagesource (strURL); MessageBox.Show ("extracting hyperlink, please wait ..."); Allinks=app.            Gethyperlinks (Strcode); MessageBox.Show ("writing file, please wait ..."); App.        Writetoxml (strURL, allinks); }

C # crawls and exports all hyperlink methods in a Web page

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.