Created
August 30, 2013 18:19
-
-
Save breyed/6392793 to your computer and use it in GitHub Desktop.
Cheese Finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using HtmlAgilityPack; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CheeseFinder | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var urlsToTry = new Queue<string>(); | |
urlsToTry.Enqueue("http://en.wikipedia.org/wiki/Farmer"); | |
var web = new HtmlWeb(); | |
while (urlsToTry.Count > 0) { | |
var url = urlsToTry.Dequeue(); | |
Console.WriteLine(url); | |
try { | |
var doc = web.Load(url); | |
var match = doc.DocumentNode.SelectSingleNode("//*[contains(.,'cheese')]"); | |
if (match != null) { | |
Process.Start(url); | |
break; | |
} | |
var nodes = doc.DocumentNode.SelectNodes("//a[@href]"); | |
foreach (var node in nodes) { | |
var newUrl = new Uri(new Uri(url), node.Attributes["href"].Value); | |
if (newUrl.Scheme == Uri.UriSchemeHttp) | |
urlsToTry.Enqueue(newUrl.AbsoluteUri); | |
} | |
} catch { } | |
} | |
Console.WriteLine("No dice."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment