Advertisement
FCN

regex vs htmlagilitypack

FCN
Sep 4th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using HtmlAgilityPack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Threading;
  10.  
  11. namespace bot_benchmark
  12. {
  13.     class Program
  14.     {
  15.         public static List<string> UrlList = new List<string>();
  16.         static void Main(string[] args)
  17.         {
  18.             Console.WriteLine("Aşağıdakilerden biri Seç");
  19.             Console.WriteLine(" htmlagilitypack \n regex ");
  20.             string selectedmethod = Console.ReadLine();
  21.             Console.WriteLine("url giriniz");
  22.             string url = Console.ReadLine();
  23.             if (selectedmethod == "htmlagilitypack")
  24.             {
  25.                 DateTime startdate = DateTime.Now;
  26.                 Console.WriteLine(startdate.ToString("h:mm:ss"));
  27.                 HtmlAgilityPackMethod(url);
  28.                 DateTime enddate = DateTime.Now;
  29.                 Console.WriteLine(enddate.ToString("h:mm:ss"));
  30.                 Console.WriteLine(enddate-startdate);
  31.                 Console.WriteLine(UrlList.Count.ToString());
  32.             }
  33.             else if (selectedmethod == "regex")
  34.             {
  35.                 DateTime startdate = DateTime.Now;
  36.                 Console.WriteLine(startdate.ToString("h:mm:ss"));
  37.                 RegexMethod(url);
  38.                 DateTime enddate = DateTime.Now;
  39.                 Console.WriteLine(enddate.ToString("h:mm:ss"));
  40.                 Console.WriteLine(enddate-startdate);
  41.                 Console.WriteLine(UrlList.Count.ToString());
  42.             }
  43.             else
  44.             {
  45.                 //Todo:
  46.             }
  47.  
  48.             Console.ReadLine();
  49.         }
  50.  
  51.        static void HtmlAgilityPackMethod(string url)
  52.         {
  53.             HtmlDocument dokuman = new HtmlDocument();
  54.             dokuman.LoadHtml(GetDownloadString(url));
  55.             HtmlNodeCollection basliklar = dokuman.DocumentNode.SelectNodes("//a[@href]");
  56.             foreach (var baslik in basliklar)
  57.             {
  58.  
  59.                 string hrefValue = baslik.GetAttributeValue("href", string.Empty);
  60.                 UrlList.Add(hrefValue);
  61.  
  62.             }
  63.         }
  64.         static void RegexMethod(string url)
  65.         {
  66.            
  67.             Regex r = new Regex(@"<(a).*?href=(""|')(.+?)(""|').*?>");
  68.             MatchCollection matchlist = r.Matches(GetDownloadString(url));
  69.             foreach (Match match in matchlist)
  70.             {
  71.                 UrlList.Add(match.Value);
  72.             }
  73.         }
  74.      
  75.         private static string GetDownloadString(string link)
  76.         {
  77.             WebClient client = new WebClient();
  78.             Uri url = new Uri(link);
  79.             client.Encoding = Encoding.UTF8;
  80.             client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  81.             string html = client.DownloadString(url);
  82.             return html;
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement