Windows

Cross-platform

控制台应用程序的快速入门

本指南展示了如何开始使用 DotNetBrowser 并将其嵌入到一个简单的控制台应用程序中。

在开始之前,请确保您的系统满足软件和硬件要求

1. 创建一个 .NET 控制台应用程序

打开终端或命令行提示符,导航到必要的目录并运行以下命令:

dotnet new console -o Example.Console

该项目将在文件夹 Example.Console 中创建。

2. 添加 DotNetBrowser 包

将目录更改为 Example.Console 并为您的操作系统添加所需的包:

Windows:

dotnet add package DotNetBrowser

Linux x64:

dotnet add package DotNetBrowser.Linux-x64

Linux ARM64:

dotnet add package DotNetBrowser.Linux-arm64

macOS x64:

dotnet add package DotNetBrowser.macOS-x64

macOS ARM64:

dotnet add package DotNetBrowser.macOS-arm64

Cross-platform:

dotnet add package DotNetBrowser.CrossPlatform

3. 更改源代码

使用任何文本编辑器将以下代码插入到 Program.cs Program.vb file:

using System;
using DotNetBrowser.Browser;
using DotNetBrowser.Dom;
using DotNetBrowser.Engine;

EngineOptions.Builder builder = new EngineOptions.Builder();
// 取消注释下面的行以指定您的许可证密钥
// builder.LicenseKey = "your_license_key";

using (IEngine engine = EngineFactory.Create(builder.Build()))
{
    IBrowser browser = engine.CreateBrowser();
    browser.Navigation
        .LoadUrl("https://quotes.toscrape.com/random").Wait();

    IDocument document = browser.MainFrame.Document;
    string quote = document.GetElementByClassName("text")?.InnerText;
    string author = document.GetElementByClassName("author")?.InnerText;

    Console.WriteLine(quote);
    Console.WriteLine($"— {author}");
}
Imports DotNetBrowser.Browser
Imports DotNetBrowser.Engine

Module Program
    Sub Main(args() As String)
        Dim builder = new EngineOptions.Builder()
        ' 取消注释下面的行以指定您的许可证密钥
        ' builder.LicenseKey = "your_license_key"

        Using engine As IEngine = EngineFactory.Create(builder.Build())
            Dim browser As IBrowser = engine.CreateBrowser()

            browser.Navigation _
                    .LoadUrl("https://quotes.toscrape.com/random").Wait()

            Dim document = browser.MainFrame.Document
            Dim quote = document.GetElementByClassName("text")?.InnerText
            Dim author = document.GetElementByClassName("author")?.InnerText

            System.Console.WriteLine(quote)
            System.Console.WriteLine($"— {author}")
        End Using
    End Sub
End Module

4. 获取试用许可证

要获得 30 天免费试用许可证,请填写网页表格并单击 “获取我的免费试用” 按钮。 您将收到一封包含许可证密钥的电子邮件。

5. 添加许可证

要将许可证密钥嵌入到您的项目中,请从电子邮件中复制许可证密钥字符串并按如下所示插入:

EngineOptions.Builder builder = new EngineOptions.Builder();
// 取消注释下面的行以指定您的许可证密钥
builder.LicenseKey = "your_license_key";
Dim builder = new EngineOptions.Builder()
' 取消注释下面的行以指定您的许可证密钥
builder.LicenseKey = "your_license_key"

有关许可证安装的更多信息,请参阅本文

6. 运行应用程序

要启动应用程序,请使用:

dotnet run

Console

Go Top