IronWebScraper 教程 C# 網路爬蟲 How to Scrape Data from Websites in C# Darrius Serrant 更新日期:6月 10, 2025 Download IronWebScraper NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English IronWebscraper 是一個.NET庫,用於網頁抓取、網頁數據提取和網頁內容解析。 這是一個易於使用的庫,可以添加到 Microsoft Visual Studio 項目中以用於開發和生產。 IronWebscraper 具有許多獨特的功能和能力,例如控制允許和禁止的頁面、對象、媒體等。它還允許管理多個身份、網頁緩存以及本教程中將介紹的許多其他功能。 開始使用 IronWebscraper 立即開始在您的項目中使用 IronWebScraper 並免費試用。 第一步: 免費啟動 目標受眾 本教程針對具有基礎或高級編程技能的軟體開發人員,他們希望構建和實現具有高級抓取能力(網站抓取、網站數據收集和提取、網站內容解析、網頁數據收集)的解決方案。 所需技能 具備使用 Microsoft 編程語言(如 C# 或 VB.NET)的編程基本知識 基本理解網絡技術(HTML、JavaScript、jQuery、CSS 等)及其運作方式 DOM、XPath、HTML 和 CSS 選擇器的基本知識 工具 Microsoft Visual Studio 2010 或更高版本 瀏覽器的 Web 開發人員擴展,例如 Chrome 的 Web 檢查器或 Firefox 的 Firebug 為什麼抓取? (原因和概念) 如果您希望構建一個具有以下功能的產品或解決方案: 提取網站數據 從多個網站比較內容、價格、功能等 掃描和緩存網站內容 如果您有上述一個或多個原因,那麼 IronWebscraper 是非常適合您需求的優秀庫 如何安裝 IronWebScraper? 在創建新項目後(見附錄 A),您可以通過自動使用 NuGet 插入庫或手動安裝 DLL 將 IronWebScraper 庫添加到您的項目中。 使用 NuGet 安裝 要使用 NuGet 將 IronWebScraper 庫添加到我們的項目中,我們可以使用可視化界面(NuGet 包管理器)或通過包管理器控制台命令來完成。 使用 NuGet 包管理器 使用鼠標 -> 右鍵單擊項目名稱 -> 選擇管理 NuGet 包 從瀏覽選項卡中 -> 搜索 IronWebScraper -> 安裝 點擊確定 完成 使用 NuGet 包控制台 從工具中 -> NuGet 包管理器 -> 包管理器控制台 選擇類庫項目為默認項目 運行命令 -> Install-Package IronWebScraper 手動安裝 前往 https://ironsoftware.com 點擊 IronWebScraper 或直接訪問其頁面 https://ironsoftware.com/csharp/webscraper/ 點擊下載 DLL。 解壓下載的壓縮文件 在 Visual Studio 中右鍵單擊項目 -> 添加 -> 引用 -> 瀏覽 前往解壓的資料夾 -> netstandard2.0 -> 選擇所有 .dll 文件 完成! HelloScraper - 我們的第一個 IronWebScraper 範例 一如既往,我們將首先實作 Hello Scraper 應用,以使用 IronWebScraper 邁出第一步。 我們已經創建了一個名為“ IronWebScraperSample”的新控制台應用 創建 IronWebScraper 範例的步驟 創建一個名為“HelloScraperSample”的資料夾 然後添加一個新類,命名為“HelloScraper” 將此代碼片段添加到 HelloScraper public class HelloScraper : WebScraper { /// <summary> /// Override this method to initialize your web scraper. /// Important tasks will be to request at least one start URL and set allowed/banned domain or URL patterns. /// </summary> public override void Init() { License.LicenseKey = "LicenseKey"; // Write License Key this.LoggingLevel = WebScraper.LogLevel.All; // Log all events this.Request("https://blog.scrapinghub.com", Parse); // Initialize a web request to the given URL } /// <summary> /// Override this method to create the default Response handler for your web scraper. /// If you have multiple page types, you can add additional similar methods. /// </summary> /// <param name="response">The HTTP Response object to parse</param> public override void Parse(Response response) { // Set working directory for the project this.WorkingDirectory = AppSetting.GetAppRoot() + @"\HelloScraperSample\Output\"; // Loop on all links foreach (var titleLink in response.Css("h2.entry-title a")) { // Read link text string title = titleLink.TextContentClean; // Save result to file Scrape(new ScrapedData() { { "Title", title } }, "HelloScraper.json"); } // Loop on all links for pagination if (response.CssExists("div.prev-post > a[href]")) { // Get next page URL var nextPage = response.Css("div.prev-post > a[href]")[0].Attributes["href"]; // Scrape next URL this.Request(nextPage, Parse); } } } public class HelloScraper : WebScraper { /// <summary> /// Override this method to initialize your web scraper. /// Important tasks will be to request at least one start URL and set allowed/banned domain or URL patterns. /// </summary> public override void Init() { License.LicenseKey = "LicenseKey"; // Write License Key this.LoggingLevel = WebScraper.LogLevel.All; // Log all events this.Request("https://blog.scrapinghub.com", Parse); // Initialize a web request to the given URL } /// <summary> /// Override this method to create the default Response handler for your web scraper. /// If you have multiple page types, you can add additional similar methods. /// </summary> /// <param name="response">The HTTP Response object to parse</param> public override void Parse(Response response) { // Set working directory for the project this.WorkingDirectory = AppSetting.GetAppRoot() + @"\HelloScraperSample\Output\"; // Loop on all links foreach (var titleLink in response.Css("h2.entry-title a")) { // Read link text string title = titleLink.TextContentClean; // Save result to file Scrape(new ScrapedData() { { "Title", title } }, "HelloScraper.json"); } // Loop on all links for pagination if (response.CssExists("div.prev-post > a[href]")) { // Get next page URL var nextPage = response.Css("div.prev-post > a[href]")[0].Attributes["href"]; // Scrape next URL this.Request(nextPage, Parse); } } } Public Class HelloScraper Inherits WebScraper ''' <summary> ''' Override this method to initialize your web scraper. ''' Important tasks will be to request at least one start URL and set allowed/banned domain or URL patterns. ''' </summary> Public Overrides Sub Init() License.LicenseKey = "LicenseKey" ' Write License Key Me.LoggingLevel = WebScraper.LogLevel.All ' Log all events Me.Request("https://blog.scrapinghub.com", AddressOf Parse) ' Initialize a web request to the given URL End Sub ''' <summary> ''' Override this method to create the default Response handler for your web scraper. ''' If you have multiple page types, you can add additional similar methods. ''' </summary> ''' <param name="response">The HTTP Response object to parse</param> Public Overrides Sub Parse(ByVal response As Response) ' Set working directory for the project Me.WorkingDirectory = AppSetting.GetAppRoot() & "\HelloScraperSample\Output\" ' Loop on all links For Each titleLink In response.Css("h2.entry-title a") ' Read link text Dim title As String = titleLink.TextContentClean ' Save result to file Scrape(New ScrapedData() From { { "Title", title } }, "HelloScraper.json") Next titleLink ' Loop on all links for pagination If response.CssExists("div.prev-post > a[href]") Then ' Get next page URL Dim nextPage = response.Css("div.prev-post > a[href]")(0).Attributes("href") ' Scrape next URL Me.Request(nextPage, AddressOf Parse) End If End Sub End Class $vbLabelText $csharpLabel 現在開始抓取,將此代碼片段添加到 Main static void Main(string[] args) { // Create Object From Hello Scrape class HelloScraperSample.HelloScraper scrape = new HelloScraperSample.HelloScraper(); // Start Scraping scrape.Start(); } static void Main(string[] args) { // Create Object From Hello Scrape class HelloScraperSample.HelloScraper scrape = new HelloScraperSample.HelloScraper(); // Start Scraping scrape.Start(); } Shared Sub Main(ByVal args() As String) ' Create Object From Hello Scrape class Dim scrape As New HelloScraperSample.HelloScraper() ' Start Scraping scrape.Start() End Sub $vbLabelText $csharpLabel 結果將保存在格式為 WebScraper.WorkingDirectory/classname.Json 的文件中 代碼概述 Scrape.Start() 觸發抓取邏輯如下: 調用 Init() 方法以初始化變數、設置抓取屬性和行為屬性。 使用 Request("https://blog.scrapinghub.com", Parse) 在 Init() 中設置起始頁請求。 在保持代碼同步和便於調試的情況下,並行處理多個 HTTP 請求和線程。 Parse() 方法在 Init() 之後被觸發,用於處理響應,使用 CSS 選擇器提取數據,並以 JSON 格式保存。 IronWebScraper 庫功能和選項 更新的文檔可以在使用手動安裝方法下載的壓縮文件中找到(IronWebScraper Documentation.chm 文件),或者您可以查看線上文檔以獲取該庫的最新更新 https://ironsoftware.com/csharp/webscraper/object-reference/。 要在項目中開始使用 IronWebScraper,您必須繼承 IronWebScraper.WebScraper 類,該類擴展了您的類庫並為其添加了抓取功能。 此外,您必須實現 Init() 和 Parse(Response response) 方法。 namespace IronWebScraperEngine { public class NewsScraper : IronWebScraper.WebScraper { public override void Init() { throw new NotImplementedException(); } public override void Parse(Response response) { throw new NotImplementedException(); } } } namespace IronWebScraperEngine { public class NewsScraper : IronWebScraper.WebScraper { public override void Init() { throw new NotImplementedException(); } public override void Parse(Response response) { throw new NotImplementedException(); } } } Namespace IronWebScraperEngine Public Class NewsScraper Inherits IronWebScraper.WebScraper Public Overrides Sub Init() Throw New NotImplementedException() End Sub Public Overrides Sub Parse(ByVal response As Response) Throw New NotImplementedException() End Sub End Class End Namespace $vbLabelText $csharpLabel 屬性 \ 功能 類型 描述 Init () 方法 用於設置抓取工具 Parse (Response response) 方法 用於實施抓取工具將使用的邏輯以及它將如何處理。 可以針對不同的頁面行為或結構實現多個方法。 BannedUrls、AllowedUrls、BannedDomains 集合 用於禁止/允許 URL 和/或域名。 例如:BannedUrls.Add("*.zip*"、"*.exe*"、"*.gz*"、"*.pdf*"); 支持通配符和正則表達式。 ObeyRobotsDotTxt 布林值 用於啟用或禁用閱讀和遵循 robots.txt 中的指令。 ObeyRobotsDotTxtForHost (string Host) 方法 用於啟用或禁用針對某個域名的 robots.txt 指令閱讀和遵循。 Scrape、ScrapeUnique 方法 ThrottleMode 枚舉 枚舉選項:ByIpAddress、ByDomainHostName。 啟用智能請求限制,尊重主機 IP 地址或域名。 EnableWebCache、EnableWebCache (TimeSpan cacheDuration) 方法 啟用對網頁請求的緩存。 MaxHttpConnectionLimit 整數 設置允許打開的 HTTP 請求(線程)的最大數量。 RateLimitPerHost 時間間隔 設置對給定域名或 IP 地址的請求之間的最小禮貌延遲(暫停)。 OpenConnectionLimitPerHost 整數 設置對每個主機名稱或 IP 地址的並發 HTTP 請求(線程)的允許數量。 WorkingDirectory 字串 設置存儲數據的工作目錄路徑。 真實世界範例和實踐 抓取在線電影網站 讓我們構建一個抓取電影網站的範例。 添加一個新類並命名為 MovieScraper: HTML 結構 這是我們在網站上看到的首頁 HTML 的一部分: <div id="movie-featured" class="movies-list movies-list-full tab-pane in fade active"> <div data-movie-id="20746" class="ml-item"> <a href="https://website.com/film/king-arthur-legend-of-the-sword-20746/"> <span class="mli-quality">CAM</span> <img data-original="https://img.gocdn.online/2017/05/16/poster/2116d6719c710eabe83b377463230fbe-king-arthur-legend-of-the-sword.jpg" class="lazy thumb mli-thumb" alt="King Arthur: Legend of the Sword" src="https://img.gocdn.online/2017/05/16/poster/2116d6719c710eabe83b377463230fbe-king-arthur-legend-of-the-sword.jpg" style="display: inline-block;"> <span class="mli-info"><h2>King Arthur: Legend of the Sword</h2></span> </a> </div> <div data-movie-id="20724" class="ml-item"> <a href="https://website.com/film/snatched-20724/" > <span class="mli-quality">CAM</span> <img data-original="https://img.gocdn.online/2017/05/16/poster/5ef66403dc331009bdb5aa37cfe819ba-snatched.jpg" class="lazy thumb mli-thumb" alt="Snatched" src="https://img.gocdn.online/2017/05/16/poster/5ef66403dc331009bdb5aa37cfe819ba-snatched.jpg" style="display: inline-block;"> <span class="mli-info"><h2>Snatched</h2></span> </a> </div> </div> <div id="movie-featured" class="movies-list movies-list-full tab-pane in fade active"> <div data-movie-id="20746" class="ml-item"> <a href="https://website.com/film/king-arthur-legend-of-the-sword-20746/"> <span class="mli-quality">CAM</span> <img data-original="https://img.gocdn.online/2017/05/16/poster/2116d6719c710eabe83b377463230fbe-king-arthur-legend-of-the-sword.jpg" class="lazy thumb mli-thumb" alt="King Arthur: Legend of the Sword" src="https://img.gocdn.online/2017/05/16/poster/2116d6719c710eabe83b377463230fbe-king-arthur-legend-of-the-sword.jpg" style="display: inline-block;"> <span class="mli-info"><h2>King Arthur: Legend of the Sword</h2></span> </a> </div> <div data-movie-id="20724" class="ml-item"> <a href="https://website.com/film/snatched-20724/" > <span class="mli-quality">CAM</span> <img data-original="https://img.gocdn.online/2017/05/16/poster/5ef66403dc331009bdb5aa37cfe819ba-snatched.jpg" class="lazy thumb mli-thumb" alt="Snatched" src="https://img.gocdn.online/2017/05/16/poster/5ef66403dc331009bdb5aa37cfe819ba-snatched.jpg" style="display: inline-block;"> <span class="mli-info"><h2>Snatched</h2></span> </a> </div> </div> HTML 如我們所見,我們有一個電影 ID、標題和指向詳細頁面的連結。 讓我們開始抓取這些數據: public class MovieScraper : WebScraper { public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; this.Request("www.website.com", Parse); } public override void Parse(Response response) { foreach (var div in response.Css("#movie-featured > div")) { if (div.GetAttribute("class") != "clearfix") { var movieId = div.GetAttribute("data-movie-id"); var link = div.Css("a")[0]; var movieTitle = link.TextContentClean; Scrape(new ScrapedData() { { "MovieId", movieId }, { "MovieTitle", movieTitle } }, "Movie.Jsonl"); } } } } public class MovieScraper : WebScraper { public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; this.Request("www.website.com", Parse); } public override void Parse(Response response) { foreach (var div in response.Css("#movie-featured > div")) { if (div.GetAttribute("class") != "clearfix") { var movieId = div.GetAttribute("data-movie-id"); var link = div.Css("a")[0]; var movieTitle = link.TextContentClean; Scrape(new ScrapedData() { { "MovieId", movieId }, { "MovieTitle", movieTitle } }, "Movie.Jsonl"); } } } } Public Class MovieScraper Inherits WebScraper Public Overrides Sub Init() License.LicenseKey = "LicenseKey" Me.LoggingLevel = WebScraper.LogLevel.All Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\" Me.Request("www.website.com", AddressOf Parse) End Sub Public Overrides Sub Parse(ByVal response As Response) For Each div In response.Css("#movie-featured > div") If div.GetAttribute("class") <> "clearfix" Then Dim movieId = div.GetAttribute("data-movie-id") Dim link = div.Css("a")(0) Dim movieTitle = link.TextContentClean Scrape(New ScrapedData() From { { "MovieId", movieId }, { "MovieTitle", movieTitle } }, "Movie.Jsonl") End If Next div End Sub End Class $vbLabelText $csharpLabel 結構化的電影類 為了保存我們格式化的數據,讓我們實現一個電影類別: public class Movie { public int Id { get; set; } public string Title { get; set; } public string URL { get; set; } } public class Movie { public int Id { get; set; } public string Title { get; set; } public string URL { get; set; } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 現在更新我們的代碼以使用電影類別: public class MovieScraper : WebScraper { public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; this.Request("https://website.com/", Parse); } public override void Parse(Response response) { foreach (var div in response.Css("#movie-featured > div")) { if (div.GetAttribute("class") != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")), Title = div.Css("a")[0].TextContentClean, URL = div.Css("a")[0].Attributes["href"] }; Scrape(movie, "Movie.Jsonl"); } } } } public class MovieScraper : WebScraper { public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; this.Request("https://website.com/", Parse); } public override void Parse(Response response) { foreach (var div in response.Css("#movie-featured > div")) { if (div.GetAttribute("class") != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")), Title = div.Css("a")[0].TextContentClean, URL = div.Css("a")[0].Attributes["href"] }; Scrape(movie, "Movie.Jsonl"); } } } } Public Class MovieScraper Inherits WebScraper Public Overrides Sub Init() License.LicenseKey = "LicenseKey" Me.LoggingLevel = WebScraper.LogLevel.All Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\" Me.Request("https://website.com/", AddressOf Parse) End Sub Public Overrides Sub Parse(ByVal response As Response) For Each div In response.Css("#movie-featured > div") If div.GetAttribute("class") <> "clearfix" Then Dim movie As New Movie With { .Id = Convert.ToInt32(div.GetAttribute("data-movie-id")), .Title = div.Css("a")(0).TextContentClean, .URL = div.Css("a")(0).Attributes("href") } Scrape(movie, "Movie.Jsonl") End If Next div End Sub End Class $vbLabelText $csharpLabel 詳細頁面抓取 讓我們擴展我們的電影類,為詳細信息添加新屬性: public class Movie { public int Id { get; set; } public string Title { get; set; } public string URL { get; set; } public string Description { get; set; } public List<string> Genre { get; set; } public List<string> Actor { get; set; } } public class Movie { public int Id { get; set; } public string Title { get; set; } public string URL { get; set; } public string Description { get; set; } public List<string> Genre { get; set; } public List<string> Actor { get; set; } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 然後導航到詳細頁面以抓取,使用擴展的 IronWebscraper 功能: public class MovieScraper : WebScraper { public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; this.Request("https://domain/", Parse); } public override void Parse(Response response) { foreach (var div in response.Css("#movie-featured > div")) { if (div.GetAttribute("class") != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")), Title = div.Css("a")[0].TextContentClean, URL = div.Css("a")[0].Attributes["href"] }; this.Request(movie.URL, ParseDetails, new MetaData() { { "movie", movie } }); } } } public void ParseDetails(Response response) { var movie = response.MetaData.Get<Movie>("movie"); var div = response.Css("div.mvic-desc")[0]; movie.Description = div.Css("div.desc")[0].TextContentClean; movie.Genre = div.Css("div > p > a").Select(element => element.TextContentClean).ToList(); movie.Actor = div.Css("div > p:nth-child(2) > a").Select(element => element.TextContentClean).ToList(); Scrape(movie, "Movie.Jsonl"); } } public class MovieScraper : WebScraper { public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; this.Request("https://domain/", Parse); } public override void Parse(Response response) { foreach (var div in response.Css("#movie-featured > div")) { if (div.GetAttribute("class") != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")), Title = div.Css("a")[0].TextContentClean, URL = div.Css("a")[0].Attributes["href"] }; this.Request(movie.URL, ParseDetails, new MetaData() { { "movie", movie } }); } } } public void ParseDetails(Response response) { var movie = response.MetaData.Get<Movie>("movie"); var div = response.Css("div.mvic-desc")[0]; movie.Description = div.Css("div.desc")[0].TextContentClean; movie.Genre = div.Css("div > p > a").Select(element => element.TextContentClean).ToList(); movie.Actor = div.Css("div > p:nth-child(2) > a").Select(element => element.TextContentClean).ToList(); Scrape(movie, "Movie.Jsonl"); } } Public Class MovieScraper Inherits WebScraper Public Overrides Sub Init() License.LicenseKey = "LicenseKey" Me.LoggingLevel = WebScraper.LogLevel.All Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\" Me.Request("https://domain/", AddressOf Parse) End Sub Public Overrides Sub Parse(ByVal response As Response) For Each div In response.Css("#movie-featured > div") If div.GetAttribute("class") <> "clearfix" Then Dim movie As New Movie With { .Id = Convert.ToInt32(div.GetAttribute("data-movie-id")), .Title = div.Css("a")(0).TextContentClean, .URL = div.Css("a")(0).Attributes("href") } Me.Request(movie.URL, AddressOf ParseDetails, New MetaData() From { { "movie", movie } }) End If Next div End Sub Public Sub ParseDetails(ByVal response As Response) Dim movie = response.MetaData.Get(Of Movie)("movie") Dim div = response.Css("div.mvic-desc")(0) movie.Description = div.Css("div.desc")(0).TextContentClean movie.Genre = div.Css("div > p > a").Select(Function(element) element.TextContentClean).ToList() movie.Actor = div.Css("div > p:nth-child(2) > a").Select(Function(element) element.TextContentClean).ToList() Scrape(movie, "Movie.Jsonl") End Sub End Class $vbLabelText $csharpLabel IronWebScraper 庫功能 HttpIdentity 功能 有些系統要求用戶登錄才能查看內容; 使用 HttpIdentity 進行憑證驗證: HttpIdentity id = new HttpIdentity { NetworkUsername = "username", NetworkPassword = "pwd" }; Identities.Add(id); HttpIdentity id = new HttpIdentity { NetworkUsername = "username", NetworkPassword = "pwd" }; Identities.Add(id); Dim id As New HttpIdentity With { .NetworkUsername = "username", .NetworkPassword = "pwd" } Identities.Add(id) $vbLabelText $csharpLabel 啟用 Web 緩存 在開發過程中緩存請求的頁面以供重用: public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\"; EnableWebCache(); this.Request("http://www.WebSite.com", Parse); } public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\"; EnableWebCache(); this.Request("http://www.WebSite.com", Parse); } Public Overrides Sub Init() License.LicenseKey = "LicenseKey" Me.LoggingLevel = WebScraper.LogLevel.All Me.WorkingDirectory = AppSetting.GetAppRoot() & "\ShoppingSiteSample\Output\" EnableWebCache() Me.Request("http://www.WebSite.com", Parse) End Sub $vbLabelText $csharpLabel 限速 控制連接數量和速度: public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\"; this.MaxHttpConnectionLimit = 80; this.RateLimitPerHost = TimeSpan.FromMilliseconds(50); this.OpenConnectionLimitPerHost = 25; this.ObeyRobotsDotTxt = false; this.ThrottleMode = Throttle.ByDomainHostName; this.Request("https://www.Website.com", Parse); } public override void Init() { License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\"; this.MaxHttpConnectionLimit = 80; this.RateLimitPerHost = TimeSpan.FromMilliseconds(50); this.OpenConnectionLimitPerHost = 25; this.ObeyRobotsDotTxt = false; this.ThrottleMode = Throttle.ByDomainHostName; this.Request("https://www.Website.com", Parse); } Public Overrides Sub Init() License.LicenseKey = "LicenseKey" Me.LoggingLevel = WebScraper.LogLevel.All Me.WorkingDirectory = AppSetting.GetAppRoot() & "\ShoppingSiteSample\Output\" Me.MaxHttpConnectionLimit = 80 Me.RateLimitPerHost = TimeSpan.FromMilliseconds(50) Me.OpenConnectionLimitPerHost = 25 Me.ObeyRobotsDotTxt = False Me.ThrottleMode = Throttle.ByDomainHostName Me.Request("https://www.Website.com", Parse) End Sub $vbLabelText $csharpLabel 限速 properties MaxHttpConnectionLimit允許打開的 HTTP 請求(線程)的總數 RateLimitPerHost對給定域名或 IP 地址的請求之間的最小禮貌延遲(以毫秒為單位) OpenConnectionLimitPerHost允許的並發 HTTP 請求(線程)數量 ThrottleMode使 WebSraper 能夠智能限速請求,不僅按主機名,還按主機伺服器的 IP 地址。 這樣可以禮貌地應對被抓取的多個域名託管在同一台機器上的情況。 附錄 如何創建 Windows 軟體應用? 使用 Visual Studio 2013 或更高版本。 打開 Visual Studio。 文件 -> 新建 -> 專案 選擇視覺 C# 或 VB -> Windows -> Windows 窗體應用。 專案名稱: IronScraperSample 位置: 在您的磁盤中選擇位置。 如何創建 ASP.NET Web Form 應用? 打開 Visual Studio。 文件 -> 新建 -> 專案 選擇視覺 C# 或 VB -> 網頁 -> ASP.NET Web 應用程序(.NET Framework)。 專案名稱: IronScraperSample 位置: 在您的磁盤中選擇位置。 從您的 ASP.NET 模板中,選擇空模板並勾選 Web Forms。 您的基本 ASP.NET Web 表單專案已創建。 在此下載完整的教程範例專案代碼專案。 常見問題解答 如何在 C# 中從網站抓取數據? 您可以使用 IronWebScraper 來在 C# 中從網站抓取數據。首先通過 NuGet 安裝該庫並設置一個基本的控制台應用程序以有效地開始提取網路數據。 C# 網路爬蟲的先決條件是什麼? 要在 C# 中執行網路爬蟲,您應具備 C# 或 VB.NET 的基本編程技能,並了解 HTML、JavaScript 和 CSS 等網絡技術,還需熟悉 DOM、XPath 和 CSS 選擇器。 如何在 .NET 項目中安裝網路爬蟲庫? 要在 .NET 項目中安裝 IronWebScraper,可使用 NuGet 封裝管理器控制台命令 Install-Package IronWebScraper,或在 Visual Studio 中瀏覽 NuGet 封裝管理器界面。 如何在我的網絡爬蟲中實現請求限制? IronWebScraper 可讓您實現請求限制來管理向伺服器發出的請求頻率。這可以通過 MaxHttpConnectionLimit、RateLimitPerHost 和 OpenConnectionLimitPerHost 等設置進行配置。 開啟網路緩存在網路爬蟲中的目的是什麼? 開啟網路緩存可以通過存儲和重用先前的響應來減少向伺服器發送的請求數。這可以在 IronWebScraper 中使用 EnableWebCache 方法進行設置。 如何在網路爬蟲中處理身份驗證? 使用 IronWebScraper,您可以使用 HttpIdentity 來管理身份驗證,以便進入登錄表單或受限區域後面的內容,從而可以抓取受保護的資源。 C# 網路爬蟲的簡單範例是什麼? 教程中提供的 'HelloScraper' 是一個簡單範例,演示了使用 IronWebScraper 設置基本網路爬蟲,包括如何發起請求和解析響應。 如何擴展我的網路爬蟲以處理複雜的頁面結構? 使用 IronWebScraper,您可以通過自定義 Parse 方法來擴展您的爬蟲以處理複雜的頁面結構,允許靈活的數據提取策略。 使用網路爬蟲庫有什麼好處? 使用像 IronWebScraper 這樣的網路爬蟲庫具有許多優勢,例如簡化數據提取、域管理、請求限制、緩存和支持身份驗證,從而使網路爬蟲任務更高效。 Darrius Serrant 立即與工程團隊聊天 全棧軟件工程師 (WebOps) Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。 準備好開始了嗎? Nuget 下載 122,916 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:122,916 查看許可證