IronWebScraper 教學 C# 中的 Web Scraping 如何使用 C# 從網站抓取數據 Darrius Serrant 更新:6月 10, 2025 下載 IronWebScraper NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronWebscraper 是一個用於網頁抓取、網頁資料擷取和網頁內容解析的 .NET 函式庫。 這是一個易於使用的庫,可以添加到 Microsoft Visual Studio 專案中,用於開發和生產。 IronWebscraper 具有許多獨特的功能和特性,例如控制允許和禁止的頁面、物件、媒體等。它還允許管理多個身分、Web 快取以及許多其他功能,我們將在本教程中介紹這些功能。 開始使用 IronWebscraper 立即開始在您的項目中使用 IronWebScraper 並免費試用。 第一步: 免費啟動 目標受眾 本教學課程針對具有基本或進階程式設計技能的軟體開發人員,他們希望建立和實現進階抓取功能(網站抓取、網站資料收集和提取、網站內容解析、網頁採集)的解決方案。 網路抓取圖片 所需技能 具備基本的程式設計知識,熟練使用微軟程式語言之一,例如 C# 或 VB.NET。 對 Web 技術(HTML、JavaScript、jQuery、CSS 等)及其運作原理有基本的了解 具備 DOM、XPath、HTML 和 CSS 選擇器的基本知識 工具 Microsoft Visual Studio 2010 或更高版本 適用於瀏覽器的 Web 開發人員擴充程序,例如 Chrome 的 Web Inspector 或 Firefox 的 Firebug。 為什麼要刮? (Reasons and Concepts) 如果您想打造一款具備以下功能的產品或解決方案: 提取網站數據 從多個網站比較內容、價格、功能等 掃描和快取網站內容 如果您有上述一個或多個原因,那麼 IronWebscraper 就是一個非常適合您需求的函式庫。 如何安裝 IronWebScraper? 建立新專案後(請參閱附錄 A),您可以透過 NuGet 自動插入程式庫或手動安裝 DLL 將 IronWebScraper 程式庫新增至您的專案。 使用 NuGet 安裝 要使用 NuGet 將 IronWebScraper 庫添加到我們的專案中,我們可以使用視覺化介面(NuGet 套件管理器)或使用套件管理器控制台的命令來完成此操作。 使用 NuGet 套件管理器 使用滑鼠 -> 右鍵點選專案名稱 -> 選擇"管理 NuGet 套件" !使用圖形使用者介面添加 IronWebscraper 從瀏覽標籤頁 -> 搜尋 IronWebScraper -> 安裝 !使用 GUI 2 新增 IronWebscraper 點選"確定" !使用 GUI 3 新增 IronWebscraper 完畢 !使用 GUI 4 新增 IronWebscraper 使用 NuGet 套件控制台 從"工具"->"NuGet 套件管理器"->"套件管理器控制台" !使用控制台新增 IronWebscraper 選擇類別庫項目作為預設項目 運行命令 -> Install-Package IronWebScraper !使用控制台新增 IronWebscraper 1 手動安裝 前往https://ironsoftware.com 點選 IronWebScraper 或直接使用 URL 造訪其頁面。https://ironsoftware.com/csharp/webscraper/ 點選下載 DLL。 解壓縮下載的壓縮文件 在 Visual Studio 中,以滑鼠右鍵按一下專案 -> 新增 -> 引用 -> 瀏覽 !使用 DLL 新增 IronWebscraper 前往解壓縮後的資料夾 -> netstandard2.0 -> 並選擇所有.dll文件 !使用 DLL 2 新增 IronWebscraper 完成了! HelloScraper - 我們的第一個 IronWebScraper 範例 像往常一樣,我們將首先實現 Hello Scraper 應用程序,並邁出使用 IronWebScraper 的第一步。 我們建立了一個名為"IronWebScraperSample"的新控制台應用程式。 建立 IronWebScraper 範例的步驟 建立一個名為"HelloScraperSample"的資料夾 然後新增一個新類,並將其命名為"HelloScraper"。 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格式的檔案中。 HelloScraper 結果 程式碼概述 Scrape.Start()會觸發如下的抓取邏輯: 呼叫Init()方法來初始化變數、抓取屬性和行為屬性。 在Init()中使用Request("https://blog.scrapinghub.com", Parse)設定起始頁面請求。 並行處理多個 HTTP 請求和線程,保持程式碼同步,更易於偵錯。 Parse()方法在Init()之後觸發,用於處理回應,使用 CSS 選擇器提取資料並將其儲存為 JSON 格式。 IronWebScraper 函式庫函數與選項 更新後的文件包含在手動安裝方法下載的壓縮檔案中( IronWebScraper Documentation.chm File ),或者您也可以查看線上文件以取得庫的最新更新。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 收藏 用於禁止/允許存取網址和/或網域名稱。 例如: 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 : 新增 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 現在更新我們的程式碼,使用 Movie 類別: 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 詳細頁面抓取 讓我們擴展 Movie 類,添加新的屬性來顯示詳細資訊: 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 節流特性 MaxHttpConnectionLimit允許開啟的 HTTP 請求(線程)總數 RateLimitPerHost向給定網域名稱或 IP 位址發出請求之間的最小禮貌延遲或暫停時間(以毫秒為單位) OpenConnectionLimitPerHost允許的並發HTTP請求(線程)數量 ThrottleMode讓 WebSraper 不僅能根據主機名,還能根據主機伺服器的 IP 位址智慧地限制請求。 如果多個被抓取的網域託管在同一台機器上,這樣做比較有禮貌。 附錄 如何建立 Windows 窗體應用程式? 請使用 Visual Studio 2013 或更高版本。 1.開啟 Visual Studio。 企業版 2015 文件 -> 新建 -> 項目 !提交新項目 選擇 View C# 或 VB -> Windows -> Windows 窗體應用程式。 建立 Windows 應用 項目名稱:IronScraperSample 位置:選擇磁碟上的一個位置。 如何建立 ASP.NET Web 窗體應用程式? 1.開啟 Visual Studio。 企業版 2015 文件 -> 新建 -> 項目 !提交新項目 選擇 View C# 或 VB -> Web -> ASP.NET Web 應用程式 (.NET Framework)。 ! ASP.NET Web 應用程式 項目名稱:IronScraperSample 位置:選擇磁碟上的一個位置。 從您的 ASP.NET 範本中,選擇一個空白範本並選取"Web 表單"。 ! ASP.NET 模板 您的基本 ASP.NET Web 表單專案已建立完成。 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 方法來處理不同的頁面類型,進而延伸您的 scraper 以處理複雜的頁面結構,讓資料擷取策略更加靈活。 使用網路搜刮程式庫有什麼好處? 使用 IronWebScraper 之類的網頁搜刮函式庫可提供多項優點,例如簡化資料擷取、網域管理、請求節制、快取以及支援驗證,讓您能有效率地處理網頁搜刮任務。 Darrius Serrant 立即與工程團隊聊天 全棧軟件工程師 (WebOps) Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。 準備好開始了嗎? Nuget 下載 125,527 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:125,527 檢視授權