简体   繁体   中英

Async parsing with AngleSharp

So I want to parse some data from website and I found a tutorial, here is code:

public static async void Test()
{
    var config = Configuration.Default.WithDefaultLoader();
    using var context = BrowsingContext.New(config);

    var url = "http://webcode.me";

    using var doc = await context.OpenAsync(url);
    // var title = doc.QuerySelector("title").InnerHtml;
    var title = doc.Title;

    Console.WriteLine(title);

    var pars = doc.QuerySelectorAll("p");

    foreach (var par in pars)
    {
        Console.WriteLine(par.Text().Trim());
    }
}

static void Main(string[] args)
{
    Test();
}

And the program quits right after it reaches the:

using var doc = await context.OpenAsync(url);

Nothing is waiting for your asynchronous method to complete, so the program quits. You can fix this by amending to use an async main method:

static Task Main(string[] args)
{
    return Test();
}

Or if you're using a version older than C# 7.1 (where async main not supported):

static void Main(string[] args)
{
    Test().GetAwaiter().GetResult();
}

You'll also need to change the return type of Test to async Task :

public static async Task Test()
{
    // ...
}

You might find the C# 7.1 docs on async main helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM