Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace dotnet_jttt_2015_jg_wg
- {
- public class Finder
- {
- private string url;
- public Finder(string adress)
- {
- url = adress;
- }
- private string GetHtml()
- {
- WebClient wc = new WebClient();
- byte[] data = wc.DownloadData(url);
- return System.Net.WebUtility.HtmlDecode(Encoding.UTF8.GetString(data));
- }
- public string findText(string textToFind)
- {
- HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
- doc.LoadHtml(GetHtml());
- var nodes = doc.DocumentNode.Descendants("img");
- string imageURL = "";
- foreach (var node in nodes)
- {
- string atribute = node.GetAttributeValue("alt", "");
- if (atribute.Contains(textToFind))
- {
- imageURL = node.GetAttributeValue("src", "");
- }
- }
- if (!imageURL.Contains("http://"))
- {
- System.ApplicationException ex = new System.ApplicationException("Nie znaleziono obrazka zawierajacego slowo \"" + textToFind + "\"");
- throw ex;
- }
- return imageURL;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement