Pi0trek

HTTPCrawler

Aug 16th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9.  
  10. namespace HTTPCrawler
  11. {
  12.     class Program
  13.     {
  14.         static HashSet<string> pages = new HashSet<string>();
  15.         static StringBuilder sb = new StringBuilder();
  16.         static int maxDepth = 1;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             //test();
  21.             //return;
  22.  
  23.             //crawlPage("http://detox.wi.ps.pl");
  24.             crawlPage("http://www.bisnode.pl/informacje/baza-maili/");
  25.             File.WriteAllText(@"E:\out.xml", sb.ToString());
  26.             Console.WriteLine("done");
  27.             Console.ReadLine();
  28.         }
  29.  
  30.         static void appendAux(string s, int depth, bool EOL = true)
  31.         {
  32.             for (int i = 0; i < depth; i++)
  33.                 sb.Append("\t");
  34.             sb.Append(s);
  35.             if (EOL)
  36.                 sb.Append(Environment.NewLine);
  37.         }
  38.  
  39.         static void crawlPage(string url, int depth = 0)
  40.         {
  41.             if (depth >= maxDepth)
  42.                 return;
  43.  
  44.             if (pages.Contains(url)) return;
  45.             pages.Add(url);
  46.  
  47.             if (depth == 0)
  48.             {
  49.                 appendAux("<?xml version=\"1.0\" encoding=\"utf‐8\" ?>", depth);
  50.                 appendAux($"<SITE url=\"{ url}\" + maxDepth={maxDepth}>", depth);
  51.             }
  52.  
  53.             string htmlCode = "";
  54.             using (WebClient client = new WebClient())
  55.                 try
  56.                 {
  57.                     htmlCode = client.DownloadString(url);
  58.                 }
  59.                 catch
  60.                 {
  61.                     return;
  62.                 }
  63.  
  64.             string[] lines = htmlCode.Split('\n');
  65.  
  66.             foreach (string line in lines) // w srodku tej petli wolamy appendAux z depth +1, żeby zaczac od wciecia juz na poczatku
  67.             {
  68.                 Match result;
  69.  
  70.                 result = Regex.Match(line, "<img (?<nie_obrazek>.*?)src=\"(?<obrazek>.+?)\"");
  71.                 if (result.Groups["obrazek"].ToString() != "")
  72.                     appendAux("<IMAGE>" + result.Groups["obrazek"] + "</IMAGE>", depth + 1);
  73.  
  74.                 result = Regex.Match(line, "href='mailto:(?<mail>.+?)'");
  75.                 if (result.Groups["mail"].ToString() != "")
  76.                     appendAux("<EMAIL>"+ result.Groups["mail"]+ "<EMAIL>", depth + 1);              
  77.                
  78.                 result = Regex.Match(line, "<a (?<nie_link>.*?)href=\"(?<link>.+?)\"");
  79.                 if (result.Groups["link"].ToString() != "")
  80.                 {
  81.                     System.Threading.Thread.Sleep(100);
  82.  
  83.                     string link = result.Groups["link"].ToString();
  84.                     appendAux("<FILE " + link + " currentDepth=" + depth + " >", depth + 1, false);
  85.                     if (link.StartsWith("http://") || link.StartsWith("https://"))
  86.                         crawlPage(link, depth + 1);
  87.                     else
  88.                         crawlPage(url + link, depth + 1);
  89.                     appendAux("</FILE>", 0); //0 zeby nie dodal tabulatora
  90.                 }
  91.              
  92.             }
  93.  
  94.             if (depth == 0)
  95.                 appendAux($"</SITE>", depth);
  96.         }
  97.  
  98.         static void test()
  99.         {
  100.             string pattern = "<img (?<nie_obrazek>.+?) src=\"(?<obrazek>.+?)\"";
  101.             string input = "<big><big><big><big><span style=\"font - weight: bold; color: red; text - decoration: blink; \"><blink></blink></span></big></big></big></big><span style=\"font - weight: bold; color: red; \"></span><img style=\"border: 0px solid; width: 640px; height: 194px; \" alt=\"\" src=\"https://leftyconcarne.files.wordpress.com/2011/12/dilbert-cartoon.gif\" usemap=\"#map\"><br>\"";
  102.  
  103.             Match result = Regex.Match(input, pattern);
  104.             Console.WriteLine("Image name: " + result.Groups["obrazek"]);
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment