Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Code Repository
   

Extract the url links from a page



 
Return

Extract the url links from a page

This code will extract the links from an html page.

  1. using System;
  2. using System.Net;
  3. using System.Text.RegularExpressions;
  4. using System.IO;
  5. class TestHarness
  6. {
  7. public static void Main()
  8. {
  9. HttpWebRequest request;
  10. HttpWebResponse response;
  11. Stream s;
  12. // create a request to the url
  13. request = (HttpWebRequest) WebRequest.Create("http://www.w3c.org/");
  14. // get the response
  15. response = (HttpWebResponse) request.GetResponse();
  16. // get the stream of data and read into a string
  17. s = response.GetResponseStream();
  18. string strContents = new StreamReader(s).ReadToEnd();
  19. Regex r = new Regex("href\\s*=\\s*(?:(?:\\\"(?<url>[^\\\"]*)\\\")|(?<url>[^\\s]* ))");
  20. MatchCollection mc1 = r.Matches(strContents);
  21. Console.WriteLine(r.ToString());
  22. foreach(Match m1 in mc1)
  23. {
  24. //Output details of Match
  25. Console.WriteLine("Match: {0}", m1.Value);
  26. //Output details of Groups
  27. foreach(Group g in m1.Groups)
  28. {
  29. Console.WriteLine("URL: {0}", g.Value);
  30. }
  31. }
  32. }
  33. }


Return
Microsoft Certified Professional   © 2018 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com