C# 中的高级网页抓取功能

Curtis Chau
Curtis Chau
更新: 2026年5月9日

HttpIdentity 功能

某些网站系统需要用户登录才能查看内容; 在这种情况下,我们可以使用一个HttpIdentity。 以下是设置方法:

// Create a new instance of HttpIdentity
HttpIdentity id = new HttpIdentity();

// Set the network username and password for authentication
id.NetworkUsername = "username";
id.NetworkPassword = "pwd";

// Add the identity to the collection of identities
Identities.Add(id);

IronWebScraper 中最令人印象深刻和强大的功能之一是能够使用成千上万个独特的用户凭据和/或浏览器引擎,通过多个登录会话来伪装或抓取网站。

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Define an array of proxies
    var proxies = "IP-Proxy1:8080,IP-Proxy2:8081".Split(',');

    // Iterate over common Chrome desktop user agents
    foreach (var UA in IronWebScraper.CommonUserAgents.ChromeDesktopUserAgents)
    {
        // Iterate over the proxies
        foreach (var proxy in proxies)
        {
            // Add a new HTTP identity with specific user agent and proxy
            Identities.Add(new HttpIdentity()
            {
                UserAgent = UA,
                UseCookies = true,
                Proxy = proxy
            });
        }
    }
    
    // Make an initial request to the website with a parse method
    this.Request("http://www.Website.com", Parse);
}

您有多个属性可以为您提供不同的行为,以防止网站屏蔽您。

这些属性包括:

  • NetworkDomain:用于用户身份验证的网络域。 支持 Windows、NTLM、Kerberos、Linux、BSD 和 Mac OS X 网络。 必须与NetworkUsername和NetworkPassword一起使用。
  • NetworkUsername:用于用户身份验证的网络/http用户名。 支持 HTTP、Windows 网络、NTLM、Kerberos、Linux 网络、BSD 网络和 Mac OS。
  • NetworkPassword:用于用户身份验证的网络/http密码。 支持 HTTP、Windows 网络、NTLM、Kerberos、Linux 网络、BSD 网络和 Mac OS。
  • Proxy:用于设置代理设置。
  • UserAgent:用于设置浏览器引擎(例如,Chrome桌面版、Chrome移动版、Chrome平板版、IE和Firefox等)。
  • HttpRequestHeaders:对于将与此身份一起使用的自定义头值,接受一个字典对象Dictionary<string, string>。
  • UseCookies:启用/禁用使用cookies。

IronWebScraper 使用随机身份运行抓取器。 如果我们需要指定使用特定的身份解析页面,可以这样做:

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Create a new instance of HttpIdentity
    HttpIdentity identity = new HttpIdentity();
    
    // Set the network username and password for authentication
    identity.NetworkUsername = "username";
    identity.NetworkPassword = "pwd";
    
    // Add the identity to the collection of identities
    Identities.Add(identity);
    
    // Make a request to the website with the specified identity
    this.Request("http://www.Website.com", Parse, identity);
}

启用网页缓存功能

此功能用于缓存请求的页面。 它通常用于开发和测试阶段,允许开发人员缓存所需页面以便在更新代码后重用。 这使您可以在重新启动 Web 抓取器后在缓存页面上执行代码,而无需每次都连接到实时网站(操作回放)。

您可以在Init()方法中使用它:

// Enable web cache without an expiration time
EnableWebCache();

// OR enable web cache with a specified expiration time
EnableWebCache(new TimeSpan(1, 30, 30));

它将把您的缓存数据保存到工作目录文件夹下的WebCache文件夹。

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Enable web cache with a specific expiration time of 1 hour, 30 minutes, and 30 seconds
    EnableWebCache(new TimeSpan(1, 30, 30));
    
    // Make an initial request to the website with a parse method
    this.Request("http://www.Website.com", Parse);
}

IronWebScraper还具有通过使用Start(CrawlID)设置引擎启动进程名称来启用引擎重启后继续抓取的功能。

static void Main(string[] args)
{
    // Create an object from the Scraper class
    EngineScraper scrape = new EngineScraper();
    
    // Start the scraping process with the specified crawl ID
    scrape.Start("enginestate");
}

执行请求和响应将保存到工作目录内的SavedState文件夹中。

节流

我们可以控制每个域的最小和最大连接数和连接速度。

public override void Init()
{
    // Set the license key for IronWebScraper
    License.LicenseKey = "LicenseKey";

    // Set the logging level to capture all logs
    this.LoggingLevel = WebScraper.LogLevel.All;

    // Assign the working directory for the output files
    this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";

    // Set the total number of allowed open HTTP requests (threads)
    this.MaxHttpConnectionLimit = 80;
    
    // Set minimum polite delay (pause) between requests to a given domain or IP address
    this.RateLimitPerHost = TimeSpan.FromMilliseconds(50);
    
    // Set the allowed number of concurrent HTTP requests (threads) per hostname or IP address
    this.OpenConnectionLimitPerHost = 25;
    
    // Do not obey the robots.txt files
    this.ObeyRobotsDotTxt = false;
    
    // Makes the WebScraper intelligently throttle requests not only by hostname, but also by host servers' IP addresses
    this.ThrottleMode = Throttle.ByDomainHostName;
    
    // Make an initial request to the website with a parse method
    this.Request("https://www.Website.com", Parse);
}

节流属性

  • MaxHttpConnectionLimit 允许打开的 HTTP 请求(线程)的总数
  • RateLimitPerHost 给定域或 IP 地址之间请求的最小礼貌延迟或暂停(毫秒)
  • OpenConnectionLimitPerHost 每个主机名允许的并发 HTTP 请求(线程)数
  • ThrottleMode 使 WebScraper 智能地限速请求,不仅按主机名,还按主机服务器的 IP 地址。 如果多个抓取的域托管在同一台机器上,这样做是礼貌的。

开始使用IronWebScraper

从今天开始在您的项目中使用 IronWebScraper 免费试用。

第一步:
arrow pointer

常见问题

在IronWebScraper中使用HttpIdentity功能的目的是什么?

IronWebScraper中的HttpIdentity功能允许用户模仿不同的用户凭据和浏览器引擎进行网页抓取,帮助通过模拟多个登录会话来绕过内容访问限制。

如何在IronWebScraper中启用网页缓存?

可以使用'EnableWebCache()'方法在IronWebScraper中启用网页缓存。这使开发者能够缓存请求的页面,特别是在开发和测试阶段,无需重复连接到网站。

在IronWebScraper中启用流量限制的优点是什么?

在IronWebScraper中启用流量限制有助于管理与域的连接次数和速度,减少因短时间内请求过多而被封锁的可能性。通过在时间上分配请求来确保礼貌和高效的抓取过程。

IronWebScraper是否可以在进程重新启动后继续抓取?

是的,IronWebScraper可以通过使用'Start(CrawlID)'命令保存当前状态,在重新启动后继续抓取进程,允许从中断处恢复。

在HttpIdentity中设置'UseCookies'属性的用途是什么?

HttpIdentity中的'UseCookies'属性允许IronWebScraper在网页抓取会话中管理cookie,使其能够在请求之间维护状态,并可能绕过网站基于会话数据设置的某些限制。

新的HttpIdentity对象为网页抓取提供了什么功能?

IronWebScraper中的HttpIdentity对象允许您控制用户代理、代理服务器和身份验证凭据,提供模拟用户行为和访问网络内容的复杂选项,适用于不同的场景。

RateLimitPerHost属性如何帮助网页抓取?

RateLimitPerHost属性确保对同一域名发出的请求之间有最小的延迟,帮助防止抓取器对服务器造成过大负担,从而降低IP被封禁的风险。

在单个抓取任务中使用多个HttpIdentity实例有什么好处?

使用多个HttpIdentity实例允许IronWebScraper在请求中循环使用不同的身份,通过不同的凭证和浏览器引擎分配负载,帮助避免检测并提高抓取任务的成功率。

为什么在IronWebScraper中设置'WorkingDirectory'很重要?

在IronWebScraper中设置'WorkingDirectory'指定输出文件、缓存数据和状态信息的存储位置。这对于组织数据和确保抓取器在其操作过程中正确访问和保存资源至关重要。

IronWebScraper能否在不断地连接到实时网站的情况下工作?

是的,IronWebScraper可以通过利用其网页缓存功能进行工作,这使其能够对缓存的页面执行操作而无需重复连接到实时网站,从而加速测试和开发周期。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 150,061版本:2026.11刚刚发布