Webscraping in C#
什麼是 IronWebScraper?
IronWebScraper 是一個適用於 C# 及 .NET Framework 程式設計平台的類別庫與框架,可讓開發人員透過程式化方式讀取網站並擷取其內容。 這非常適合用於對網站或現有內部網路進行逆向工程,並將其轉化為資料庫或 JSON 資料。 它對於從網路上下載大量文件也很有幫助。
在許多方面,IronWebScraper 與 Python 的 Scrapy 函式庫相似,但它利用了 C# 的優勢,特別是能在網頁抓取過程中逐步執行程式碼並進行除錯的能力。
安裝
您的第一步是安裝 IronWebScraper,您可以透過 NuGet 安裝,或從我們的網站下載 DLL 檔案。
您所需的所有類別皆可於 IronWebScraper 命名空間中找到。
常見應用情境
將網站遷移至資料庫
IronWebScraper 提供工具與方法,讓您能將網站內容重新建構為結構化資料庫。 此技術在將舊有網站和內部網路的內容遷移至您的新 C# 應用程式時非常實用。
網站遷移
能夠透過 C# 輕鬆擷取部分或整個網站的內容,可降低遷移或升級網站及內部網路資源所耗費的時間與成本。 這比直接進行 SQL 轉換要高效得多,因為它將資料扁平化為網頁上可見的內容,既無需理解先前的 SQL 資料結構,也無需編寫複雜的 SQL 查詢。
建立搜尋索引
您可以將 IronWebScraper 指向您自己的網站或內部網路,以讀取結構化資料、遍歷每個頁面,並擷取正確的資料,從而讓貴組織內的搜尋引擎能準確地建立資料庫。
IronWebScraper 是為您的搜尋索引抓取內容的理想工具。 像 IronSearch 這樣的搜尋應用程式,可以讀取 IronWebScraper 提供的結構化內容,藉此建構強大的 Enterprise 搜尋系統。
使用 Iron WEBSCRAPER
若要了解如何使用 IronWebScraper,建議參考範例。 此基本範例建立了一個類別,用於從網站部落格擷取標題。
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
Class MainClass
Public Shared Sub Main(ByVal args As String())
Dim scraper As New BlogScraper()
scraper.Start()
End Sub
End Class
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
' 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 NamespaceImports IronWebScraper
Namespace WebScrapingProject
Class MainClass
Public Shared Sub Main(ByVal args As String())
Dim scraper As New BlogScraper()
scraper.Start()
End Sub
End Class
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
' 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若要抓取特定網站,我們必須建立專屬的類別來讀取該網站。此類別將繼承自 Web Scraper。 我們將為此類別新增若干方法,包括 Init,藉此設定初始參數並發起首次請求,進而引發連鎖反應,最終完成整個網站的抓取作業。
此外,我們還必須加入至少一個 Parse 方法。 解析方法會讀取從網際網路下載的網頁,並使用類似 jQuery 的 CSS 選擇器來選取內容,並擷取相關的文字和/或圖片以供使用。
在 Parse 方法中,我們亦可指定希望爬蟲繼續追蹤哪些超連結,以及忽略哪些超連結。
我們可以使用 Scrape 方法擷取任何資料,並將其匯出為方便使用的 JSON 樣式檔案格式,以供日後使用。
後續進行
若要進一步了解 IronWebScraper,建議您先閱讀 API 參考文件,然後開始參考文件中"教學"章節內的範例。
我們建議您參考的下一則範例是 C#"部落格"網頁抓取範例,其中我們將學習如何從部落格(例如 WordPress 部落格)中擷取文字內容。 這在網站遷移過程中可能會非常有用。
接著,您可以進一步參考其他進階的WEBSCRAPER教學範例,其中將探討諸如包含多種不同頁面型別的網站、電子商務網站等概念,以及在從網路上抓取資料時如何使用多個代理伺服器、身分識別與登入帳戶。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。