Created
January 10, 2020 09:23
-
-
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)
This file contains 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 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