IronWebScraper 開始使用 Webscraping in C# Darrius Serrant 更新日期:6月 8, 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? IronWebScraper是一個用於C#和.NET編程平台的類庫和框架,允許開發者以編程方式讀取網站並提取其內容。 這對於逆向工程網站或現有內聯網並將其轉回資料庫或JSON數據非常理想。 它還適用於從互聯網下載大量文檔。 在許多方面,Iron Web Scraper類似於Python的Scrapy庫,但利用了C#的優勢,特別是其在網站抓取過程中可以逐步通過代碼進行調試的能力。 安裝 Your first step will be to install Iron Web Scraper, which you may do from NuGet or by downloading the DLL from our website. 您需要的所有類都可以在Iron Web Scraper命名空間中找到。 Install-Package IronWebScraper 熱門使用案例 將網站遷移到資料庫 IronWebScraper提供工具和方法,允許您重新設計網站,恢復成結構化的資料庫。 當將內容從傳統網站和內聯網遷移到新的C#應用程序時,這項技術非常有用。 網站遷移 能夠輕鬆提取部分或完整網站的內容到C#中,减少了在遷移或升級網站和內聯網資源過程中的時間和成本問題。 這可能比直SQL轉換更高效,因為它將數據簡化為每個網頁上看到的內容,不需要瞭解之前的SQL數據結構,也不用構建複雜SQL查詢。 填充搜索索引 Iron Web Scraper可指向您自己的网站或内联网读取结构化数据,读取每个页面,并提取正确的数据以便组织内部的搜索引擎能够被准确填充。 IronWebScraper是抓取您搜索索引内容的理想工具。 像IronSearch这样的搜索应用程序可以从IronWebScraper读取结构化内容,以构建强大的企业搜索系统。 使用Iron Webscraper 要学习如何使用Iron Web Scraper,最好查看一些示例。 这个基本示例创建一个类来抓取网站博客的标题。 using IronWebScraper; namespace WebScrapingProject { class MainClass { public static void Main(string [] args) { var scraper = new BlogScraper(); scraper.Start(); } } class BlogScraper : WebScraper { // Initialize scraper settings and make the first request public override void Init() { // Set logging level to show all log messages this.LoggingLevel = WebScraper.LogLevel.All; // Request the initial page to start scraping this.Request("https://ironpdf.com/blog/", Parse); } // Method to handle parsing of the page response public override void Parse(Response response) { // Loop through each blog post title link found by CSS selector foreach (var title_link in response.Css("h2.entry-title a")) { // Clean and extract the title text string strTitle = title_link.TextContentClean; // Store the extracted title for later use Scrape(new ScrapedData() { { "Title", strTitle } }); } // Check if there is a link to the previous post page and if exists, follow it if (response.CssExists("div.prev-post > a[href]")) { // Get the URL for the next page var next_page = response.Css("div.prev-post > a[href]")[0].Attributes["href"]; // Request the next page to continue scraping this.Request(next_page, Parse); } } } } using IronWebScraper; namespace WebScrapingProject { class MainClass { public static void Main(string [] args) { var scraper = new BlogScraper(); scraper.Start(); } } class BlogScraper : WebScraper { // Initialize scraper settings and make the first request public override void Init() { // Set logging level to show all log messages this.LoggingLevel = WebScraper.LogLevel.All; // Request the initial page to start scraping this.Request("https://ironpdf.com/blog/", Parse); } // Method to handle parsing of the page response public override void Parse(Response response) { // Loop through each blog post title link found by CSS selector foreach (var title_link in response.Css("h2.entry-title a")) { // Clean and extract the title text string strTitle = title_link.TextContentClean; // Store the extracted title for later use Scrape(new ScrapedData() { { "Title", strTitle } }); } // Check if there is a link to the previous post page and if exists, follow it if (response.CssExists("div.prev-post > a[href]")) { // Get the URL for the next page var next_page = response.Css("div.prev-post > a[href]")[0].Attributes["href"]; // Request the next page to continue scraping this.Request(next_page, Parse); } } } } Imports IronWebScraper Namespace WebScrapingProject Friend Class MainClass Public Shared Sub Main(ByVal args() As String) Dim scraper = New BlogScraper() scraper.Start() End Sub End Class Friend Class BlogScraper Inherits WebScraper ' Initialize scraper settings and make the first request Public Overrides Sub Init() ' Set logging level to show all log messages Me.LoggingLevel = WebScraper.LogLevel.All ' Request the initial page to start scraping Me.Request("https://ironpdf.com/blog/", AddressOf Parse) End Sub ' Method to handle parsing of the page response Public Overrides Sub Parse(ByVal response As Response) ' Loop through each blog post title link found by CSS selector For Each title_link In response.Css("h2.entry-title a") ' Clean and extract the title text Dim strTitle As String = title_link.TextContentClean ' Store the extracted title for later use Scrape(New ScrapedData() From { { "Title", strTitle } }) Next title_link ' Check if there is a link to the previous post page and if exists, follow it If response.CssExists("div.prev-post > a[href]") Then ' Get the URL for the next page Dim next_page = response.Css("div.prev-post > a[href]")(0).Attributes("href") ' Request the next page to continue scraping Me.Request(next_page, AddressOf Parse) End If End Sub End Class End Namespace $vbLabelText $csharpLabel 要抓取特定网站,我们需要创建自己的类来读取该网站。该类将扩展Web Scraper。 我们将为该类添加一些方法,包括Init,我们可以在其中设置初始设置并开始第一个请求,这将依次引发一系列反应,整个网站将被抓取。 我们还必须至少添加一个Parse方法。 Parse方法读取从互联网上下载的网页,使用类似jQuery的CSS选择器选择内容并提取相关文本和/或图像以供使用。 在Parse方法中,我们还可以指定希望抓取程序继续跟踪哪些超链接以及忽略哪些超链接。 我们可以使用抓取方法提取任何数据,并将其转储到方便的JSON样式文件格式中以供以后使用。 前進 要了解有關Iron Web Scraper的更多信息,我們建議您閱讀API參考文檔,然後開始查看我們文檔的教程部分中的示例。 我們建議您查看的下一個示例是C#“博客”網頁爬蟲示例,我們可以從中學習如何從博客中提取文本內容,例如WordPress博客。 這在網站遷移中可能非常有用。 從那裡,您可能會繼續查看其他高級網頁爬蟲教程示例,在那裡我們可以查看諸如具有許多不同類型頁面的網站、電子商務網站的概念,還有如何在從互聯網抓取數據時使用多個代理、身份和登錄。 Darrius Serrant 立即與工程團隊聊天 全棧軟件工程師 (WebOps) Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。 準備好開始了嗎? Nuget 下載 122,916 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:122,916 查看許可證