Skip to content

Instantly share code, notes, and snippets.

@breyed
Created August 30, 2013 18:19
Show Gist options
  • Save breyed/6392793 to your computer and use it in GitHub Desktop.
Save breyed/6392793 to your computer and use it in GitHub Desktop.
Cheese Finder
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