Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace dotnet_jttt_2015_jg_wg
  9. {
  10.     public class Finder
  11.     {
  12.         private string url;
  13.  
  14.         public Finder(string adress)
  15.         {
  16.             url = adress;
  17.         }
  18.  
  19.         private string GetHtml()
  20.         {
  21.             WebClient wc = new WebClient();
  22.             byte[] data = wc.DownloadData(url);
  23.             return System.Net.WebUtility.HtmlDecode(Encoding.UTF8.GetString(data));
  24.         }
  25.  
  26.         public string findText(string textToFind)
  27.         {
  28.             HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  29.             doc.LoadHtml(GetHtml());
  30.             var nodes = doc.DocumentNode.Descendants("img");
  31.             string imageURL = "";
  32.             foreach (var node in nodes)
  33.             {
  34.                 string atribute = node.GetAttributeValue("alt", "");
  35.                 if (atribute.Contains(textToFind))
  36.                 {
  37.                     imageURL = node.GetAttributeValue("src", "");
  38.                 }
  39.             }
  40.             if (!imageURL.Contains("http://"))
  41.             {
  42.                 System.ApplicationException ex = new System.ApplicationException("Nie znaleziono obrazka zawierajacego slowo \"" + textToFind + "\"");
  43.                 throw ex;
  44.             }
  45.             return imageURL;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement