Guest User

Webcrawler

a guest
Apr 14th, 2011
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Text;
  7.  
  8. namespace WebCrawler
  9. {
  10.     class HtmlDocRegEx : IHTMLDoc
  11.     {
  12.         private IList<String> _links;
  13.         private String _doc;
  14.        
  15.         //constructor
  16.         public HtmlDocRegEx(String url)
  17.         {
  18.             this.URL = url;
  19.             HtmlParser();
  20.         }
  21.  
  22.         public HtmlDocRegEx(String url, String doc)
  23.         {
  24.             this.URL = url;
  25.             this._doc = doc;
  26.             HtmlParser();
  27.         }      
  28.  
  29.         //set doc value/parse
  30.         public String Doc
  31.         {
  32.             get
  33.             {
  34.                 return _doc;
  35.             }
  36.             set
  37.             {
  38.                 _doc = value;
  39.                 HtmlParser();
  40.             }
  41.         }
  42.  
  43.         //Get/sets.
  44.         public String URL { get; set; }
  45.         public String Title{ get; set; }
  46.  
  47.         //GetAllLinks
  48.         public IList<string> GetAllLinks()
  49.         {
  50.             return _links;
  51.         }
  52.        
  53.         //Searchstring
  54.         public bool Contains(string searchString)
  55.         {
  56.             throw new NotImplementedException();
  57.         }
  58.          
  59.         //Parser html
  60.         private void HtmlParser()
  61.         {
  62.             this._links = GetLinks(this._doc, this.URL);
  63.             this.Title = GetTitle(this._doc);
  64.         }
  65.  
  66.         public static string GetTitle(string args)
  67.         {
  68.             string result = "<Ingen titel>";
  69.             string regEx = "(?i)<title>(?<name>.*?)</title>";
  70.             Regex regObj = new Regex(regEx);
  71.             Match match = regObj.Match(args);
  72.             if(match.Success)
  73.             {
  74.                 result = match.Groups["name"].Value;
  75.             }
  76.             return result;
  77.         }
  78.  
  79.         private static IList<string> GetLinks(string args, string calledUri)
  80.         {
  81.             List<string> _links = new List<string>();
  82.             IList<string> _linksFinal = new List<string>();
  83.             _links.AddRange(GetLinksHelper(args, "a"));
  84.             _links.AddRange(GetLinksHelper(args, "frame"));
  85.             _links.AddRange(GetLinksHelper(args, "iframe"));
  86.  
  87.             foreach (string link in _links)
  88.             {
  89.                 string result = link;
  90.                 string protocol = (link.StartsWith("https")) ? "https" : "http";
  91.  
  92.                 if (link.StartsWith("javascript:") || link.StartsWith("#") || link.StartsWith("mailto:"))
  93.                 {
  94.  
  95.                 }
  96.                 else if (!link.StartsWith("http"))
  97.                 {
  98.                     string path = link;
  99.  
  100.                     if (!path.StartsWith("/"))
  101.                     {
  102.                         Uri baseUri = new Uri(calledUri);
  103.                         string basePath = baseUri.AbsolutePath;
  104.  
  105.                         if (!basePath.EndsWith("/"))
  106.                         {
  107.                             int index = basePath.LastIndexOf("/");
  108.                             basePath = basePath.Remove(index);
  109.                         }
  110.  
  111.                         path = basePath + "/" + path;
  112.                         path = path.Replace(@"./", @"/");
  113.                         path = path.Replace(@"//", @"/");
  114.                     }
  115.  
  116.                     UriBuilder uri = new UriBuilder(protocol, GetUriHostname(calledUri), 80, path);
  117.                     _linksFinal.Add(uri.ToString().Replace("%3F", "?"));
  118.                 }
  119.                 else if (DomainEquals(result, calledUri))
  120.                 {
  121.                     _linksFinal.Add(result);
  122.                 }
  123.             }
  124.  
  125.             return _linksFinal;
  126.         }
  127.  
  128.         public static bool DomainEquals(string uri1, string uri2)
  129.         {
  130.             return GetUriHostname(uri1).Equals(GetUriHostname(uri2));
  131.         }
  132.  
  133.         public static string GetUriHostname(string uri)
  134.         {
  135.             Uri host = new Uri(uri);
  136.             return host.Host;
  137.         }
  138.  
  139.         private static List<string> GetLinksHelper(string args, string type)
  140.         {
  141.             List<string> result = new List<string>();
  142.  
  143.             //string regularExpression = "(?i)<a.*?href=[\"\"'](?<url>.*?)[\"\"'].*?>(?<name>.*?)</a>";
  144.             string regularExpression = "(?i)<a.*?href=[\"\"'](?<url>.*?)[\"\"'].*?>";
  145.             if (type.Equals("frame"))
  146.             {
  147.                 regularExpression = "(?i)<frame.*?src=[\"\"'](?<url>.*?)[\"\"'].*?>";
  148.             }
  149.             else if (type.Equals("iframe"))
  150.             {
  151.                 regularExpression = "(?i)<iframe.*?src=[\"\"'](?<url>.*?)[\"\"'].*?>";
  152.             }
  153.             Regex regObj = new Regex(regularExpression);
  154.  
  155.             MatchCollection matches = regObj.Matches(args);
  156.             foreach (Match mat in matches)
  157.             {
  158.                 string st = mat.Groups["url"].Value;
  159.                 result.Add(st);
  160.             }
  161.  
  162.             return result;
  163.         }
  164.  
  165.  
  166.  
  167.         public string Document
  168.         {
  169.             get { throw new NotImplementedException(); }
  170.         }
  171.  
  172.          IList IHTMLDoc.GetAllLinks()
  173.         {
  174.             throw new NotImplementedException();
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment