IronWebScraper 如何 抓取在線電影網站 使用 C# 和 IronWebScraper 抓取線上電影網站 Darrius Serrant 更新:2026年1月10日 下載 IronWebScraper NuGet 下載 DLL 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronWebScraper 透過解析 HTML 元素、創建用於結構化資料儲存的類型化物件以及使用元資料在頁面之間導航,從網站中提取電影數據,從而建立全面的電影資訊資料集。 這個C# 網路爬蟲庫簡化了將非結構化網路內容轉換為有組織、可分析的資料的過程。 快速入門:用 C# 抓取影片素材 透過NuGet套件管理器安裝 IronWebScraper 建立一個繼承自 WebScraper 的類 重寫 Init() 以設定許可證並請求目標 URL 重寫 Parse() 使用 CSS 選擇器擷取影片數據 使用 Scrape() 方法以 JSON 格式儲存數據 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronWebScraper PM > Install-Package IronWebScraper 複製並運行這段程式碼。 using IronWebScraper; using System; public class QuickstartMovieScraper : WebScraper { public override void Init() { // Set your license key License.LicenseKey = "YOUR-LICENSE-KEY"; // Configure scraper settings this.LoggingLevel = LogLevel.All; this.WorkingDirectory = @"C:\MovieData\Output\"; // Start scraping from the homepage this.Request("https://example-movie-site.com", Parse); } public override void Parse(Response response) { // Extract movie titles using CSS selectors foreach (var movieDiv in response.Css(".movie-item")) { var title = movieDiv.Css("h2")[0].TextContentClean; var url = movieDiv.Css("a")[0].Attributes["href"]; // Save the scraped data Scrape(new { Title = title, Url = url }, "movies.json"); } } } // Run the scraper var scraper = new QuickstartMovieScraper(); scraper.Start(); 部署到您的生產環境進行測試 今天就在您的專案中開始使用免費試用IronWebScraper Free 30 Day Trial 如何設定電影抓取類別? 先從一個真實的網站案例開始。 我們將使用我們在 C# 網路爬蟲教程中概述的技術來抓取電影網站。 新增一個類別並將其命名為 MovieScraper: 建立專門的爬蟲類有助於組織程式碼並使其可重複使用。 這種方法遵循物件導向的原則,並且允許您以後輕鬆擴展功能。 目標網站的架構是什麼樣的? 檢查網站結構,看看是否可以進行資料抓取。 了解網站結構對於有效進行網路爬蟲至關重要。 與我們關於 從線上電影網站抓取資料的指南類似,首先分析 HTML 結構: 哪些HTML元素包含電影資料? 這是網站首頁HTML程式碼的一部分。分析HTML結構有助於確定要使用的正確CSS選擇器: <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、片名和詳細頁面的連結。 每部電影都包含在一個帶有類別名稱 ml-item 的 data-movie-id 元素中,並包含一個唯一的 data-movie-id 屬性用於標識。 如何實現基本的電影資料抓取? 開始抓取此資料集。 在執行任何爬蟲程序之前,請確保已按如下所示正確配置您的許可證密鑰: public class MovieScraper : WebScraper { public override void Init() { // Initialize scraper settings License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; // Request homepage content for scraping this.Request("www.website.com", Parse); } public override void Parse(Response response) { // Iterate over each movie div within the featured movie section foreach (var div in response.Css("#movie-featured > div")) { if (div.Attributes["class"] != "clearfix") { var movieId = Convert.ToInt32(div.GetAttribute("data-movie-id")); var link = div.Css("a")[0]; var movieTitle = link.TextContentClean; // Scrape and store movie data as key-value pairs Scrape(new ScrapedData() { { "MovieId", movieId }, { "MovieTitle", movieTitle } }, "Movie.Jsonl"); } } } } public class MovieScraper : WebScraper { public override void Init() { // Initialize scraper settings License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; // Request homepage content for scraping this.Request("www.website.com", Parse); } public override void Parse(Response response) { // Iterate over each movie div within the featured movie section foreach (var div in response.Css("#movie-featured > div")) { if (div.Attributes["class"] != "clearfix") { var movieId = Convert.ToInt32(div.GetAttribute("data-movie-id")); var link = div.Css("a")[0]; var movieTitle = link.TextContentClean; // Scrape and store movie data as key-value pairs Scrape(new ScrapedData() { { "MovieId", movieId }, { "MovieTitle", movieTitle } }, "Movie.Jsonl"); } } } } $vbLabelText $csharpLabel 工作目錄屬性是做什麼用的? 這段程式碼有什麼新內容? 工作目錄屬性設定所有抓取資料和相關文件的主工作目錄。 這樣可以確保所有輸出檔案都集中在一個位置,從而更容易管理大規模的抓取專案。 如果目錄不存在,則會自動建立。 何時應該使用 CSS 選擇器,何時該使用屬性? 其他注意事項: 當需要根據元素的結構位置或類別名稱來定位元素時,CSS 選擇器是理想的選擇;而直接屬性存取更適合提取特定值,例如 ID 或自訂資料屬性。 在我們的範例中,我們使用 CSS 選擇器 (#movie-featured > div) 來導覽 DOM 結構,並使用屬性 (data-movie-id) 來提取特定值。 如何為抓取的資料建立類型化物件? 建構類型化對象,以格式化對象的形式保存抓取的資料。 使用強型別物件可以提供更好的程式碼組織、IntelliSense 支援和編譯時類型檢查。 實作一個用於保存格式化資料的類別: 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; } } $vbLabelText $csharpLabel 使用類型化物件如何改善資料組織? 更新程式碼,使用類型化的 Movie 類,而不是通用的 ScrapedData 字典: public class MovieScraper : WebScraper { public override void Init() { // Initialize scraper settings License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; // Request homepage content for scraping this.Request("https://website.com/", Parse); } public override void Parse(Response response) { // Iterate over each movie div within the featured movie section foreach (var div in response.Css("#movie-featured > div")) { if (div.Attributes["class"] != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")) }; var link = div.Css("a")[0]; movie.Title = link.TextContentClean; movie.URL = link.Attributes["href"]; // Scrape and store movie object Scrape(movie, "Movie.Jsonl"); } } } } public class MovieScraper : WebScraper { public override void Init() { // Initialize scraper settings License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; // Request homepage content for scraping this.Request("https://website.com/", Parse); } public override void Parse(Response response) { // Iterate over each movie div within the featured movie section foreach (var div in response.Css("#movie-featured > div")) { if (div.Attributes["class"] != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")) }; var link = div.Css("a")[0]; movie.Title = link.TextContentClean; movie.URL = link.Attributes["href"]; // Scrape and store movie object Scrape(movie, "Movie.Jsonl"); } } } } $vbLabelText $csharpLabel Scrape 方法對類型化物件使用什麼格式? 什麼是新的? 我們實作了一個 Movie 類別來保存抓取的數據,從而提供了類型安全性和更好的程式碼組織。 我們將電影物件傳遞給 Scrape 方法,該方法能夠理解我們的格式並以定義的方式保存,如下所示: 輸出結果會自動序列化為 JSON 格式,方便匯入資料庫或其他應用程式。 如何抓取電影的詳細頁面資訊? 開始抓取更詳細的頁面。 多頁面抓取是一個常見的需求,IronWebScraper 透過其請求鏈機制使其變得簡單易行。 我還能從詳情頁面提取哪些其他數據? 電影頁面如下所示,其中包含每部電影的豐富元數據: <div class="mvi-content"> <div class="thumb mvic-thumb" style="background-image: url(https://img.gocdn.online/2017/04/28/poster/5a08e94ba02118f22dc30f298c603210-guardians-of-the-galaxy-vol-2.jpg);"></div> <div class="mvic-desc"> <h3>Guardians of the Galaxy Vol. 2</h3> <div class="desc"> Set to the backdrop of Awesome Mixtape #2, Marvel's Guardians of the Galaxy Vol. 2 continues the team's adventures as they travel throughout the cosmos to help Peter Quill learn more about his true parentage. </div> <div class="mvic-info"> <div class="mvici-left"> <p> <strong>Genre: </strong> <a href="https://Domain/genre/action/" title="Action">Action</a>, <a href="https://Domain/genre/adventure/" title="Adventure">Adventure</a>, <a href="https://Domain/genre/sci-fi/" title="Sci-Fi">Sci-Fi</a> </p> <p> <strong>Actor: </strong> <a target="_blank" href="https://Domain/actor/chris-pratt" title="Chris Pratt">Chris Pratt</a>, <a target="_blank" href="https://Domain/actor/-zoe-saldana" title="Zoe Saldana">Zoe Saldana</a>, <a target="_blank" href="https://Domain/actor/-dave-bautista-" title="Dave Bautista">Dave Bautista</a> </p> <p> <strong>Director: </strong> <a href="#" title="James Gunn">James Gunn</a> </p> <p> <strong>Country: </strong> <a href="https://Domain/country/us" title="United States">United States</a> </p> </div> <div class="mvici-right"> <p><strong>Duration:</strong> 136 min</p> <p><strong>Quality:</strong> <span class="quality">CAM</span></p> <p><strong>Release:</strong> 2017</p> <p><strong>IMDb:</strong> 8.3</p> </div> <div class="clearfix"></div> </div> <div class="clearfix"></div> </div> <div class="clearfix"></div> </div> <div class="mvi-content"> <div class="thumb mvic-thumb" style="background-image: url(https://img.gocdn.online/2017/04/28/poster/5a08e94ba02118f22dc30f298c603210-guardians-of-the-galaxy-vol-2.jpg);"></div> <div class="mvic-desc"> <h3>Guardians of the Galaxy Vol. 2</h3> <div class="desc"> Set to the backdrop of Awesome Mixtape #2, Marvel's Guardians of the Galaxy Vol. 2 continues the team's adventures as they travel throughout the cosmos to help Peter Quill learn more about his true parentage. </div> <div class="mvic-info"> <div class="mvici-left"> <p> <strong>Genre: </strong> <a href="https://Domain/genre/action/" title="Action">Action</a>, <a href="https://Domain/genre/adventure/" title="Adventure">Adventure</a>, <a href="https://Domain/genre/sci-fi/" title="Sci-Fi">Sci-Fi</a> </p> <p> <strong>Actor: </strong> <a target="_blank" href="https://Domain/actor/chris-pratt" title="Chris Pratt">Chris Pratt</a>, <a target="_blank" href="https://Domain/actor/-zoe-saldana" title="Zoe Saldana">Zoe Saldana</a>, <a target="_blank" href="https://Domain/actor/-dave-bautista-" title="Dave Bautista">Dave Bautista</a> </p> <p> <strong>Director: </strong> <a href="#" title="James Gunn">James Gunn</a> </p> <p> <strong>Country: </strong> <a href="https://Domain/country/us" title="United States">United States</a> </p> </div> <div class="mvici-right"> <p><strong>Duration:</strong> 136 min</p> <p><strong>Quality:</strong> <span class="quality">CAM</span></p> <p><strong>Release:</strong> 2017</p> <p><strong>IMDb:</strong> 8.3</p> </div> <div class="clearfix"></div> </div> <div class="clearfix"></div> </div> <div class="clearfix"></div> </div> HTML 我應該如何擴展我的電影類以添加其他屬性? 使用新屬性(Duration、@@--CODE-611-6-8-6-8-6-8-8-8-6-8-8-8-6-8-8-8-6-8-614-68 類,但僅使用-6-6 Genre 和 Actor。 使用 List<string> 表示類型和演員可以優雅地處理多個值: using System.Collections.Generic; 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; } } using System.Collections.Generic; 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; } } $vbLabelText $csharpLabel 如何在抓取資料時進行頁面切換? 導覽至詳情頁面進行抓取。 IronWebScraper 會自動處理執行緒安全性問題,允許同時處理多個頁面。 為什麼對不同類型的頁面使用不同的解析函數? IronWebScraper 允許新增多個抓取函數來處理不同的頁面格式。 這種關注點分離使你的程式碼更易於維護,並允許對不同的頁面結構進行適當的處理。 每個解析函數都可以專注於從特定頁面類型中提取資料。 元資料如何幫助在解析函數之間傳遞物件? 元資料功能對於維護請求之間的狀態至關重要。 如需了解更多進階網頁抓取功能,請查看我們的詳細指南: public class MovieScraper : WebScraper { public override void Init() { // Initialize scraper settings License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; // Request homepage content for scraping this.Request("https://domain/", Parse); } public override void Parse(Response response) { // Iterate over each movie div within the featured movie section foreach (var div in response.Css("#movie-featured > div")) { if (div.Attributes["class"] != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")) }; var link = div.Css("a")[0]; movie.Title = link.TextContentClean; movie.URL = link.Attributes["href"]; // Request detailed page this.Request(movie.URL, ParseDetails, new MetaData() { { "movie", movie } }); } } } public void ParseDetails(Response response) { // Retrieve movie object from metadata var movie = response.MetaData.Get<Movie>("movie"); var div = response.Css("div.mvic-desc")[0]; // Extract description movie.Description = div.Css("div.desc")[0].TextContentClean; // Extract genres movie.Genre = new List<string>(); // Initialize genre list foreach(var genre in div.Css("div > p > a")) { movie.Genre.Add(genre.TextContentClean); } // Extract actors movie.Actor = new List<string>(); // Initialize actor list foreach (var actor in div.Css("div > p:nth-child(2) > a")) { movie.Actor.Add(actor.TextContentClean); } // Scrape and store detailed movie data Scrape(movie, "Movie.Jsonl"); } } public class MovieScraper : WebScraper { public override void Init() { // Initialize scraper settings License.LicenseKey = "LicenseKey"; this.LoggingLevel = WebScraper.LogLevel.All; this.WorkingDirectory = AppSetting.GetAppRoot() + @"\MovieSample\Output\"; // Request homepage content for scraping this.Request("https://domain/", Parse); } public override void Parse(Response response) { // Iterate over each movie div within the featured movie section foreach (var div in response.Css("#movie-featured > div")) { if (div.Attributes["class"] != "clearfix") { var movie = new Movie { Id = Convert.ToInt32(div.GetAttribute("data-movie-id")) }; var link = div.Css("a")[0]; movie.Title = link.TextContentClean; movie.URL = link.Attributes["href"]; // Request detailed page this.Request(movie.URL, ParseDetails, new MetaData() { { "movie", movie } }); } } } public void ParseDetails(Response response) { // Retrieve movie object from metadata var movie = response.MetaData.Get<Movie>("movie"); var div = response.Css("div.mvic-desc")[0]; // Extract description movie.Description = div.Css("div.desc")[0].TextContentClean; // Extract genres movie.Genre = new List<string>(); // Initialize genre list foreach(var genre in div.Css("div > p > a")) { movie.Genre.Add(genre.TextContentClean); } // Extract actors movie.Actor = new List<string>(); // Initialize actor list foreach (var actor in div.Css("div > p:nth-child(2) > a")) { movie.Actor.Add(actor.TextContentClean); } // Scrape and store detailed movie data Scrape(movie, "Movie.Jsonl"); } } $vbLabelText $csharpLabel 這種多頁面抓取方法的主要特點是什麼? 什麼是新的? 新增抓取函數(例如,ParseDetails)來抓取詳細頁面,類似於從購物網站抓取時使用的技術。 將產生檔案的 Scrape 函數移至新函數,確保僅在收集所有詳細資料後才儲存資料。 使用 IronWebScraper 功能(MetaData)將影片物件傳遞給新的抓取函數,並在請求之間保持物件狀態。 抓取網頁並將影片物件資料儲存到包含完整資訊的文件中。 有關可用方法和屬性的更多信息,請參閱API 參考文件。 IronWebScraper 提供了一個強大的框架,用於從網站中提取結構化數據,使其成為資料收集和分析專案的重要工具。 常見問題解答 如何使用 C# 從 HTML 擷取電影標題? IronWebScraper 提供 CSS 選擇器方法來從 HTML 擷取電影標題。使用 response.Css() 方法與適當的選擇器,如 '.movie-item h2' 來鎖定標題元素,然後訪問 TextContentClean 屬性來取得乾淨的文字值。 在多個電影頁面之間導覽的最佳方式是什麼? IronWebScraper 透過 Request() 方法處理頁面導覽。您可以使用 CSS 選擇器來擷取分頁連結,然後以每個 URL 來呼叫 Request(),從多個頁面中搜刮資料,自動建立全面的影片資料集。 如何將擷取的影片資料儲存為結構化格式? 使用 IronWebScraper 的 Scrape() 方法以 JSON 格式儲存資料。建立匿名物件或包含影片屬性(如標題、URL 和評分)的類型,然後將它們連同檔案名稱傳送給 Scrape(),以自動序列化並儲存資料。 我應該使用哪些 CSS 選擇器來擷取影片資訊? IronWebScraper 支援標準 CSS 選擇器。對於電影網站,可使用'.movie-item「等選擇器來表示容器、」h2「表示標題、」a[href]'表示連結,以及特定的類別名稱來表示評級或類型。Css() 方法會返回您可以遍歷的集合。 如何處理 scraped 資料中的「CAM」等電影品質指標? IronWebScraper 可讓您針對其特定的 HTML 元素,擷取並處理品質指標。使用 CSS 選擇器來定位品質徽章或文字,然後將它們作為屬性包含在您的 scraped 資料物件中,以獲得全面的電影資訊。 我可以為我的影片搜刮作業設定記錄嗎? 是的,IronWebScraper 包含內建的日誌功能。在 Init() 方法中將 LoggingLevel 屬性設定為 LogLevel.All,以追蹤所有的刮除活動、錯誤和進度,這有助於調試和監控您的影片資料擷取。 為 scraped data 設定工作目錄的正確方式是什麼? IronWebScraper 可讓您在 Init() 方法中設定一個 WorkingDirectory 屬性。指定一個路徑,例如「C:\MovieData\Output\」,將擷取的影片資料檔案儲存於此。這可集中管理輸出,並讓您的資料井井有條。 如何正確繼承 WebScraper 類別? 創建一個繼承自 IronWebScraper 的 WebScraper 基類的新類。覆寫 Init() 方法進行設定,並覆寫 Parse() 方法進行資料擷取邏輯。這種面向物件的方法可讓您的影片搜刮程式可重複使用且易於維護。 Darrius Serrant 立即與工程團隊聊天 全棧軟件工程師 (WebOps) Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。 準備好開始了嗎? Nuget 下載 131,860 | 版本: 2026.3 剛剛發布 開始免費試用 免費 NuGet 下載 總下載量:131,860 查看許可證 還在捲動嗎? 想要快速證明? PM > Install-Package IronWebScraper 執行範例 觀看您的目標網站成為結構化資料。 免費 NuGet 下載 總下載量:131,860 查看許可證