Bitzsoft.Integrations.CrawlingService 1.0.0-alpha.7

This is a prerelease version of Bitzsoft.Integrations.CrawlingService.
dotnet add package Bitzsoft.Integrations.CrawlingService --version 1.0.0-alpha.7
                    
NuGet\Install-Package Bitzsoft.Integrations.CrawlingService -Version 1.0.0-alpha.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.CrawlingService" Version="1.0.0-alpha.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.CrawlingService" Version="1.0.0-alpha.7" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.CrawlingService" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.CrawlingService --version 1.0.0-alpha.7
                    
#r "nuget: Bitzsoft.Integrations.CrawlingService, 1.0.0-alpha.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.CrawlingService@1.0.0-alpha.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.CrawlingService&version=1.0.0-alpha.7&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.CrawlingService&version=1.0.0-alpha.7&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.CrawlingService

网页爬取服务集成客户端 -- 基于 Selenium 的浏览器自动化,支持 Chrome / Firefox / Edge 多浏览器切换。

功能特性

  • 支持 Chrome、Firefox、Edge 三大浏览器引擎
  • 无头(Headless)与有头模式自由切换
  • 完整的元素定位体系:ID、CSS 选择器、XPath、Name、ClassName、TagName
  • 显式等待链式 API:等待可见、可点击、页面加载等
  • 丰富的交互操作:输入、单击、悬停、右键、双击
  • JavaScript 执行、截图、Frame / Window / Alert / Cookie 管理
  • Options 强类型配置,支持 IConfigurationSection 绑定
  • IDisposable 生命周期管理,浏览器资源自动释放

安装

dotnet add package Bitzsoft.Integrations.CrawlingService

或直接在项目文件中引用:

<PackageReference Include="Bitzsoft.Integrations.CrawlingService" Version="*" />

配置

appsettings.json 中添加爬取服务配置:

{
  "Crawling": {
    "Enabled": true,
    "BrowserType": "Chrome",
    "Headless": true,
    "DefaultWaitSeconds": 30,
    "PageLoadTimeoutSeconds": 60,
    "ScriptTimeoutSeconds": 30,
    "DriverPath": "/usr/local/bin/chromedriver",
    "UserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    "WindowWidth": 1920,
    "WindowHeight": 1080
  }
}

注册服务

using Bitzsoft.Integrations.CrawlingService;

// 方式一:通过 IConfigurationSection 绑定
builder.Services.AddCrawlingService(builder.Configuration.GetSection("Crawling"));

// 方式二:通过委托手动配置
builder.Services.AddCrawlingService(options =>
{
    options.Enabled = true;
    options.BrowserType = BrowserType.Chrome;
    options.Headless = true;
    options.DefaultWaitSeconds = 30;
    options.PageLoadTimeoutSeconds = 60;
    options.ScriptTimeoutSeconds = 30;
    options.DriverPath = "/usr/local/bin/chromedriver";
    options.WindowWidth = 1920;
    options.WindowHeight = 1080;
});

使用示例

以下示例展示登录第三方网站并抓取订单列表数据:

using Bitzsoft.Integrations.CrawlingService;

/// <summary>
/// 第三方订单爬取服务
/// </summary>
public class OrderCrawler : IDisposable
{
    private readonly ICrawlingService _crawler;

    /// <summary>
    /// 初始化订单爬取服务实例
    /// </summary>
    /// <param name="crawler">浏览器自动化服务(由 DI 注入)</param>
    public OrderCrawler(ICrawlingService crawler)
    {
        _crawler = crawler;
    }

    /// <summary>
    /// 登录目标网站并获取订单摘要列表
    /// </summary>
    /// <param name="username">登录用户名</param>
    /// <param name="password">登录密码</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>订单摘要文本列表</returns>
    public async Task<List<string>> FetchOrderSummariesAsync(
        string username,
        string password,
        CancellationToken cancellationToken = default)
    {
        // 导航到登录页
        await _crawler.OpenUrl("https://example-supplier.com/login");

        // 等待登录表单加载并填写凭据
        await _crawler.WaitForElement("#username", cancellationToken: cancellationToken);
        await _crawler.InputText("#username", username);
        await _crawler.InputText("#password", password);

        // 提交登录
        await _crawler.Click("#btn-login");

        // 等待跳转到首页后进入订单页
        await _crawler.WaitForPageLoad(cancellationToken: cancellationToken);
        await _crawler.OpenUrl("https://example-supplier.com/orders");

        // 等待订单表格渲染完成
        await _crawler.WaitForVisible("table.order-list", cancellationToken: cancellationToken);

        // 提取每行订单摘要
        var rows = await _crawler.FindByCssSelector("table.order-list tbody tr");
        var summaries = new List<string>();

        foreach (var row in rows)
        {
            string text = await _crawler.GetText(row);
            summaries.Add(text.Trim());
        }

        // 截图存档
        await _crawler.TakeScreenshot("screenshots/orders.png");

        return summaries;
    }

    /// <summary>
    /// 释放浏览器资源
    /// </summary>
    public void Dispose()
    {
        _crawler?.Dispose();
    }
}

相关包

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.CrawlingService:

Package Downloads
Bitzsoft.Integrations.All

Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.7 56 6/16/2026
1.0.0-alpha.6 55 6/16/2026
1.0.0-alpha.5 57 6/14/2026
1.0.0-alpha.3 52 6/7/2026
1.0.0-alpha.2 60 5/29/2026
1.0.0-alpha.1 53 5/28/2026