📜  C# 抓取 html 文档 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:11.299000             🧑  作者: Mango

代码示例1
// install first using 
// Install-Package AngleSharp
// then
using System;
using AngleSharp;
using AngleSharp.Html.Parser;

class MyClass {
    static async Task Main() {
        //Use the default configuration for AngleSharp
        var config = Configuration.Default;

        //Create a new context for evaluating webpages with the given config
        var context = BrowsingContext.New(config);

        //Source to be parsed
        var source = "

Some example source

This is a paragraph element"; //Create a virtual request to specify the document to load (here from our fixed string) var document = await context.OpenAsync(req => req.Content(source)); //Do something with document like the following Console.WriteLine("Serializing the (original) document:"); Console.WriteLine(document.DocumentElement.OuterHtml); var p = document.CreateElement("p"); p.TextContent = "This is another paragraph."; Console.WriteLine("Inserting another element in the body ..."); document.Body.AppendChild(p); Console.WriteLine("Serializing the document again:"); Console.WriteLine(document.DocumentElement.OuterHtml); } }