在 C# 中的網路爬蟲
IronWebScraper 是什麼?
IronWebScraper 是一個適用於 C# 和 .NET 編程平台的類庫與架構,允許開發者以程式化的方式讀取網站並提取其內容。這對於逆向工程網站或現有內聯網,並將其轉換回資料庫或 JSON 數據非常理想。它還適用於從互聯網下載大量文檔。
在許多方面,IronWebScraper 與 Python 的 Scrapy 庫相似,但利用了 C# 的優勢,特別是它能夠在網頁抓取過程中逐步執行代碼並進行調試。
安裝
您的第一步將是安裝 IronWebscraper,您可以從 NuGet 或由 下載DLL 從我們的網站。
您所有需要的類別都可以在 Iron Web Scraper 命名空間中找到。
PM > Install-Package IronWebScraper
熱門使用案例
將網站遷移到數據庫
IronWebScraper 提供工具和方法,允許您將網站重新設計為結構化數據庫。這項技術在將舊有網站和內聯網的內容遷移到您的新 C# 應用程序中非常有用。
遷移網站
在C#中能夠輕鬆提取部分或整個網站的內容,可以減少遷移或升級網站和內聯網資源的時間和成本。這比直接的SQL轉換效率更高,因為它將數據扁平化為每個網頁上可以看到的內容,並且不需要理解先前的SQL數據結構,也不需要建立複雜的SQL查詢。
填充搜索索引
Iron Web Scraper 可以指向自己的網站或內聯網以讀取結構化數據,讀取每個頁面,並提取正確數據,使您組織內的搜索引擎能夠準確填充。
IronWebScraper 是抓取搜索索引內容的理想工具。像 IronSearch 這樣的搜索應用程序可以從 IronWebScraper 讀取結構化內容,以建立一個強大的企業搜索系統。
使用 Iron Webscraper
要學習如何使用 Iron Webscraper,最好查看範例。這個基本範例創建了一個類來抓取網站博客的標題。
using IronWebScraper;
namespace WebScrapingProject
{
class MainClass
{
public static void Main(string [] args)
{
var scraper = new BlogScraper();
scraper.Start();
}
}
class BlogScraper : WebScraper
{
public override void Init()
{
this.LoggingLevel = WebScraper.LogLevel.All;
this.Request("https://ironpdf.com/blog/", Parse);
}
public override void Parse(Response response)
{
foreach (var title_link in response.Css("h2.entry-title a"))
{
string strTitle = title_link.TextContentClean;
Scrape(new ScrapedData() { { "Title", strTitle } });
}
if (response.CssExists("div.prev-post > a [href]"))
{
var next_page = response.Css("div.prev-post > a [href]")[0].Attributes ["href"];
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
{
public override void Init()
{
this.LoggingLevel = WebScraper.LogLevel.All;
this.Request("https://ironpdf.com/blog/", Parse);
}
public override void Parse(Response response)
{
foreach (var title_link in response.Css("h2.entry-title a"))
{
string strTitle = title_link.TextContentClean;
Scrape(new ScrapedData() { { "Title", strTitle } });
}
if (response.CssExists("div.prev-post > a [href]"))
{
var next_page = response.Css("div.prev-post > a [href]")[0].Attributes ["href"];
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
Public Overrides Sub Init()
Me.LoggingLevel = WebScraper.LogLevel.All
Me.Request("https://ironpdf.com/blog/", AddressOf Parse)
End Sub
Public Overrides Sub Parse(ByVal response As Response)
For Each title_link In response.Css("h2.entry-title a")
Dim strTitle As String = title_link.TextContentClean
Scrape(New ScrapedData() From {
{ "Title", strTitle }
})
Next title_link
If response.CssExists("div.prev-post > a [href]") Then
Dim next_page = response.Css("div.prev-post > a [href]")(0).Attributes ("href")
Me.Request(next_page, AddressOf Parse)
End If
End Sub
End Class
End Namespace
要抓取特定網站,我們需要創建一個自己的類來讀取該網站。這個類將繼承Web Scraper。 我們會給這個類添加一些方法,包括init
,在這裡我們可以設置初始設定並開始第一個請求,這將引發連鎖反應,從而抓取整個網站。
我們還必須至少添加一個 Parse
方法。Parse方法會讀取從互聯網下載的網頁,並使用類似jQuery的CSS選擇器來選擇內容並提取相關的文本和/或圖像以供使用。
在 Parse
方法內,我們還可以指定我們希望爬蟲繼續跟踪的超連結,以及哪些超連結將被忽略。
我們可以使用scrape方法來提取任何數據,並將其轉儲到方便使用的JSON樣式文件格式中以供後期使用。
向前進
若要了解更多有關Iron Web Scraper的信息,我們建議您閱讀 API參考文件然後開始查看我們文檔教程部分中的範例。
我們建議您查看的下一個範例是 C# "博客" 網頁刮取範例在這裡,我們學習如何從博客(例如WordPress博客)提取文本內容。這在網站遷移中可能非常有用。
從這裡開始,您可能會繼續查看其他內容。 高級網頁抓取教程 例如,我們可以觀察一些概念,例如具有多種類型頁面的網站、電子商務網站,以及在從互聯網抓取數據時如何使用多個代理、身份和登錄。