Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Created January 10, 2020 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsscoder/350998503650d7fc389a0b4d268a5cdf to your computer and use it in GitHub Desktop.
Save gsscoder/350998503650d7fc389a0b4d268a5cdf to your computer and use it in GitHub Desktop.
Easy C# program that scrapes Google with AngleSharp (part of a blog post)
using System;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Html.Dom;
using AngleSharp.Dom;
class Program
{
static async Task Main(string[] args)
{
var context = BrowsingContext.New(Configuration.Default.WithDefaultLoader());
using (var document = await context.OpenAsync("https://www.google.com/")) {
var form = document.QuerySelector<IHtmlFormElement>("form[action='/search']");
using (var result = await form.SubmitAsync(new { q = "bill gates" }))
{
var links = result.QuerySelectorAll<IHtmlAnchorElement>("a");
foreach (var link in links) {
var url = link.Attributes["href"].Value;
Console.WriteLine(url);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment