Extract a hyperlink from a Web page (C #)

Source: Internet
Author: User
Tags foreach datetime tostring
Links | web using System;
Using System.Xml;
Using System.Text;
Using System.Net;
Using System.IO;
Using System.Collections;
Using System.Text.RegularExpressions;

public class APP
{
public static void Main ()
{
String Strcode;
ArrayList allinks;

Console.Write ("Please enter a Web address:");
String strurl = Console.ReadLine ();
if (strurl.substring (0,7)!= @ "http://")
{
strURL = @ "http://" + strurl;
}

Console.WriteLine ("Getting the page code, please wait ...");
Strcode = Getpagesource (strURL);

Console.WriteLine ("Fetching hyperlinks, please wait ...");
Allinks = Gethyperlinks (Strcode);

Console.WriteLine ("Writing file, please wait ...");
Writetoxml (strurl,allinks);
}

Gets the HTML code for the specified Web page
static string Getpagesource (String URL)
{
Uri Uri =new uri (URL);

HttpWebRequest Hwreq = (HttpWebRequest) webrequest.create (URI);
HttpWebResponse hwres = (HttpWebResponse) hwreq.getresponse ();

Hwreq.method = "Get";

Hwreq.keepalive = false;

StreamReader reader = new StreamReader (Hwres.getresponsestream (), System.Text.Encoding.GetEncoding ("GB2312"));

Return reader. ReadToEnd ();
}

Extract URLs from HTML code
Static ArrayList Gethyperlinks (String htmlcode)
{
ArrayList al = new ArrayList ();

String Strregex = @ "http://" ([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)? ";

Regex r = new Regex (strregex,regexoptions.ignorecase);
MatchCollection m = r.matches (Htmlcode);

for (int i=0; i<=m.count-1; i++)
{
BOOL rep = false;
String strnew = M[i]. ToString ();

Filter for duplicate URLs
foreach (String str in AL)
{
if (STRNEW==STR)
{
Rep =true;
Break
}
}

if (!rep) al. ADD (strnew);
}

Al. Sort ();

Return al;
}

Write URLs to XML files
static void Writetoxml (String strurl, ArrayList alhyperlinks)
{
XmlTextWriter writer = new XmlTextWriter ("Hyperlinks.xml", Encoding.UTF8);

Writer. formatting = formatting.indented;
Writer. WriteStartDocument (FALSE);
Writer. Writedoctype ("HyperLinks", NULL, "URLS.DTD", null);
Writer. WriteComment ("Extract from" + strURL + "HYPERLINK");
Writer. WriteStartElement ("HyperLinks");
Writer. WriteStartElement ("HyperLinks", null);
Writer. WriteAttributeString ("DateTime", DateTime.Now.ToString ());


foreach (String str in alhyperlinks)
{
string title = GetDomain (str);
string BODY = str;
Writer. WriteElementString (Title,null,body);
}

Writer. WriteEndElement ();
Writer. WriteEndElement ();

Writer. Flush ();
Writer. Close ();
}

Get the domain name suffix of the URL
static string GetDomain (String strurl)
{
String RetVal;

String Strregex = @ "(\.com/|\.net/|\.cn/|\.org/|\.gov/)";

Regex r = new Regex (strregex,regexoptions.ignorecase);
Match m = R.match (strURL);
RetVal = M.tostring ();

Strregex = @ "\.| /$";
RetVal = Regex.Replace (RetVal, Strregex, ""). ToString ();

if (RetVal = "")
RetVal = "other";

return retVal;
}
}



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.