使用C#和IronWebScraper抓取在线电影网站
IronWebScraper 通过解析 HTML 元素从网站提取电影数据,创建类型化对象以存储结构化数据,并利用元数据在页面间导航,构建完整的电影信息数据集。这个 C# 网页抓取 库可将非结构化网页内容转换为有条理、便于分析的数据。
快速入门:使用 C# 抓取电影数据- 通过NuGet包管理器安装
IronWebScraper - 创建一个继承自
WebScraper的类 - 重写
Init()以设置许可证并请求目标URL - 重写
Parse()使用CSS选择器来提取电影数据 - 使用
Scrape()方法以JSON格式保存数据
-
1使用 IronWebScraper 安装 NuGet Package Manager
-
2复制并运行这段代码。
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();Imports IronWebScraper Imports System Public Class QuickstartMovieScraper Inherits WebScraper Public Overrides Sub Init() ' Set your license key License.LicenseKey = "YOUR-LICENSE-KEY" ' Configure scraper settings Me.LoggingLevel = LogLevel.All Me.WorkingDirectory = "C:\MovieData\Output\" ' Start scraping from the homepage Me.Request("https://example-movie-site.com", AddressOf Parse) End Sub Public Overrides Sub Parse(response As Response) ' Extract movie titles using CSS selectors For Each movieDiv In response.Css(".movie-item") Dim title = movieDiv.Css("h2")(0).TextContentClean Dim url = movieDiv.Css("a")(0).Attributes("href") ' Save the scraped data Scrape(New With {Key .Title = title, Key .Url = url}, "movies.json") Next End Sub End Class ' Run the scraper Dim scraper As New QuickstartMovieScraper() scraper.Start()Imports IronWebScraper Imports System Public Class QuickstartMovieScraper Inherits WebScraper Public Overrides Sub Init() ' Set your license key License.LicenseKey = "YOUR-LICENSE-KEY" ' Configure scraper settings Me.LoggingLevel = LogLevel.All Me.WorkingDirectory = "C:\MovieData\Output\" ' Start scraping from the homepage Me.Request("https://example-movie-site.com", AddressOf Parse) End Sub Public Overrides Sub Parse(response As Response) ' Extract movie titles using CSS selectors For Each movieDiv In response.Css(".movie-item") Dim title = movieDiv.Css("h2")(0).TextContentClean Dim url = movieDiv.Css("a")(0).Attributes("href") ' Save the scraped data Scrape(New With {Key .Title = title, Key .Url = url}, "movies.json") Next End Sub End Class ' Run the scraper Dim scraper As New QuickstartMovieScraper() scraper.Start() -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronWebScraper
如何设置一个电影抓取器类?
从真实世界的网站示例开始。 我们将使用在我们的 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>
可以从中获取电影 ID、标题和详情页链接。每部电影对应一个类名为 ml-item 的 div 元素,其中包含用于标识该电影的唯一 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");
}
}
}
}Public Class MovieScraper
Inherits WebScraper
Public Overrides Sub Init()
' Initialize scraper settings
License.LicenseKey = "LicenseKey"
Me.LoggingLevel = WebScraper.LogLevel.All
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\"
' Request homepage content for scraping
Me.Request("www.website.com", AddressOf Parse)
End Sub
Public Overrides Sub Parse(ByVal response As Response)
' Iterate over each movie div within the featured movie section
For Each div In response.Css("#movie-featured > div")
If div.Attributes("class") <> "clearfix" Then
Dim movieId = Convert.ToInt32(div.GetAttribute("data-movie-id"))
Dim link = div.Css("a")(0)
Dim movieTitle = link.TextContentClean
' Scrape and store movie data as key-value pairs
Scrape(New ScrapedData() From {
{"MovieId", movieId},
{"MovieTitle", movieTitle}
}, "Movie.Jsonl")
End If
Next
End Sub
End ClassPublic Class MovieScraper
Inherits WebScraper
Public Overrides Sub Init()
' Initialize scraper settings
License.LicenseKey = "LicenseKey"
Me.LoggingLevel = WebScraper.LogLevel.All
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\"
' Request homepage content for scraping
Me.Request("www.website.com", AddressOf Parse)
End Sub
Public Overrides Sub Parse(ByVal response As Response)
' Iterate over each movie div within the featured movie section
For Each div In response.Css("#movie-featured > div")
If div.Attributes("class") <> "clearfix" Then
Dim movieId = Convert.ToInt32(div.GetAttribute("data-movie-id"))
Dim link = div.Css("a")(0)
Dim movieTitle = link.TextContentClean
' Scrape and store movie data as key-value pairs
Scrape(New ScrapedData() From {
{"MovieId", movieId},
{"MovieTitle", movieTitle}
}, "Movie.Jsonl")
End If
Next
End Sub
End Class工作目录属性有什么作用?
此代码的新增内容是什么?
Working Directory 属性用于设置抓取数据和相关文件的主工作目录。将所有输出文件集中在同一位置,便于管理大规模抓取项目。如果目录不存在,程序会自动创建。
何时使用 CSS 选择器,何时直接读取属性?
额外的注意事项:
CSS 选择器是通过结构位置或类名来定位元素的理想选择,而直接属性访问则更适合提取 ID 或自定义数据属性等特定值。 在我们的示例中,我们使用CSS选择器(#movie-featured > div)导航DOM结构,并使用属性(data-movie-id)提取特定值。
如何为抓取数据创建类型化对象?
创建类型化对象,以结构化的形式保存抓取的数据。使用强类型对象有助于组织代码,并提供 IntelliSense 支持和编译时类型检查。
实现一个 Movie 类来保存格式化的数据:
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; }
}Public Class Movie
Public Property Id As Integer
Public Property Title As String
Public Property URL As String
End ClassPublic Class Movie
Public Property Id As Integer
Public Property Title As String
Public Property URL As String
End Class使用类型化对象如何改善数据组织?
更新代码以使用类型化的 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");
}
}
}
}Public Class MovieScraper
Inherits WebScraper
Public Overrides Sub Init()
' Initialize scraper settings
License.LicenseKey = "LicenseKey"
Me.LoggingLevel = WebScraper.LogLevel.All
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\"
' Request homepage content for scraping
Me.Request("https://website.com/", AddressOf Parse)
End Sub
Public Overrides Sub Parse(response As Response)
' Iterate over each movie div within the featured movie section
For Each div In response.Css("#movie-featured > div")
If div.Attributes("class") <> "clearfix" Then
Dim movie As New Movie With {
.Id = Convert.ToInt32(div.GetAttribute("data-movie-id"))
}
Dim link = div.Css("a")(0)
movie.Title = link.TextContentClean
movie.URL = link.Attributes("href")
' Scrape and store movie object
Scrape(movie, "Movie.Jsonl")
End If
Next
End Sub
End ClassPublic Class MovieScraper
Inherits WebScraper
Public Overrides Sub Init()
' Initialize scraper settings
License.LicenseKey = "LicenseKey"
Me.LoggingLevel = WebScraper.LogLevel.All
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\"
' Request homepage content for scraping
Me.Request("https://website.com/", AddressOf Parse)
End Sub
Public Overrides Sub Parse(response As Response)
' Iterate over each movie div within the featured movie section
For Each div In response.Css("#movie-featured > div")
If div.Attributes("class") <> "clearfix" Then
Dim movie As New Movie With {
.Id = Convert.ToInt32(div.GetAttribute("data-movie-id"))
}
Dim link = div.Css("a")(0)
movie.Title = link.TextContentClean
movie.URL = link.Attributes("href")
' Scrape and store movie object
Scrape(movie, "Movie.Jsonl")
End If
Next
End Sub
End Class抓取方法为类型化对象使用什么格式?
这段代码有哪些变化?
- 我们实现了一个
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>
如何扩展电影类以添加更多属性?
扩展 Movie 类,添加 Description、Genre、Actor、Director、Country、Duration 和 IMDb Score 属性。本示例仅使用 Description、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; }
}Imports System.Collections.Generic
Public Class Movie
Public Property Id() As Integer
Public Property Title() As String
Public Property URL() As String
Public Property Description() As String
Public Property Genre() As List(Of String)
Public Property Actor() As List(Of String)
End ClassImports System.Collections.Generic
Public Class Movie
Public Property Id() As Integer
Public Property Title() As String
Public Property URL() As String
Public Property Description() As String
Public Property Genre() As List(Of String)
Public Property Actor() As List(Of String)
End Class如何在抓取时在页面之间导航?
访问详情页并抓取其中的数据。IronWebScraper 会自动处理线程安全问题,支持并发处理多个页面。
为什么要为不同的页面类型使用多个解析函数?
IronWebScraper 支持添加多个抓取函数,以处理不同的页面格式。这样可以按页面结构分别处理数据,便于维护代码。每个解析函数可以专门提取一种页面类型的数据。
元数据如何帮助在解析函数之间传递对象?
MetaData 功能对于在请求之间保持状态至关重要。 有关更多 高级网站抓取功能,请查看我们的详细指南:
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");
}
}Public Class MovieScraper
Inherits WebScraper
Public Overrides Sub Init()
' Initialize scraper settings
License.LicenseKey = "LicenseKey"
Me.LoggingLevel = WebScraper.LogLevel.All
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\"
' Request homepage content for scraping
Me.Request("https://domain/", AddressOf Parse)
End Sub
Public Overrides Sub Parse(response As Response)
' Iterate over each movie div within the featured movie section
For Each div In response.Css("#movie-featured > div")
If div.Attributes("class") <> "clearfix" Then
Dim movie As New Movie With {
.Id = Convert.ToInt32(div.GetAttribute("data-movie-id"))
}
Dim link = div.Css("a")(0)
movie.Title = link.TextContentClean
movie.URL = link.Attributes("href")
' Request detailed page
Me.Request(movie.URL, AddressOf ParseDetails, New MetaData() From {{"movie", movie}})
End If
Next
End Sub
Public Sub ParseDetails(response As Response)
' Retrieve movie object from metadata
Dim movie = response.MetaData.Get(Of Movie)("movie")
Dim div = response.Css("div.mvic-desc")(0)
' Extract description
movie.Description = div.Css("div.desc")(0).TextContentClean
' Extract genres
movie.Genre = New List(Of String)() ' Initialize genre list
For Each genre In div.Css("div > p > a")
movie.Genre.Add(genre.TextContentClean)
Next
' Extract actors
movie.Actor = New List(Of String)() ' Initialize actor list
For Each actor In div.Css("div > p:nth-child(2) > a")
movie.Actor.Add(actor.TextContentClean)
Next
' Scrape and store detailed movie data
Scrape(movie, "Movie.Jsonl")
End Sub
End ClassPublic Class MovieScraper
Inherits WebScraper
Public Overrides Sub Init()
' Initialize scraper settings
License.LicenseKey = "LicenseKey"
Me.LoggingLevel = WebScraper.LogLevel.All
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\MovieSample\Output\"
' Request homepage content for scraping
Me.Request("https://domain/", AddressOf Parse)
End Sub
Public Overrides Sub Parse(response As Response)
' Iterate over each movie div within the featured movie section
For Each div In response.Css("#movie-featured > div")
If div.Attributes("class") <> "clearfix" Then
Dim movie As New Movie With {
.Id = Convert.ToInt32(div.GetAttribute("data-movie-id"))
}
Dim link = div.Css("a")(0)
movie.Title = link.TextContentClean
movie.URL = link.Attributes("href")
' Request detailed page
Me.Request(movie.URL, AddressOf ParseDetails, New MetaData() From {{"movie", movie}})
End If
Next
End Sub
Public Sub ParseDetails(response As Response)
' Retrieve movie object from metadata
Dim movie = response.MetaData.Get(Of Movie)("movie")
Dim div = response.Css("div.mvic-desc")(0)
' Extract description
movie.Description = div.Css("div.desc")(0).TextContentClean
' Extract genres
movie.Genre = New List(Of String)() ' Initialize genre list
For Each genre In div.Css("div > p > a")
movie.Genre.Add(genre.TextContentClean)
Next
' Extract actors
movie.Actor = New List(Of String)() ' Initialize actor list
For Each actor In div.Css("div > p:nth-child(2) > a")
movie.Actor.Add(actor.TextContentClean)
Next
' Scrape and store detailed movie data
Scrape(movie, "Movie.Jsonl")
End Sub
End Class这种多页面抓取方法的关键特性是什么?
这段代码有哪些变化?
- 添加抓取函数(例如
ParseDetails)以抓取详情页,做法类似于从购物网站抓取数据。 - 将生成文件的
Scrape函数移入新的函数,确保数据仅在收集完整细节后保存。 - 使用 IronWebScraper 的
MetaData功能将电影对象传给新的抓取函数,以便在不同请求之间保留对象状态。 - 抓取页面并将电影对象数据保存到包含完整信息的文件中。
有关可用方法和属性的更多信息,请查阅 API参考。 IronWebScraper 提供了一个可靠的框架,用于从网站中提取结构化数据,使其成为数据收集和分析项目的必要工具。
常见问题
IronWebScraper在C#中用于做什么?
IronWebScraper用于从网站中提取电影数据,通过解析HTML元素并创建结构化数据集,大大简化了将非结构化网络内容转换为有组织数据的过程。
如何在C#项目中安装IronWebScraper?
您可以通过NuGet包管理器在C#项目中安装IronWebScraper,方便地将其集成到您的项目中进行网页抓取任务。
抓取电影网站时通常使用哪些CSS选择器?
CSS选择器用于在电影网站的HTML结构中导航和选择元素。例如,.movie-item类可以用来选择包含电影信息(如标题或URLs)的div标签。
IronWebScraper能处理多页爬取吗?
是的,IronWebScraper能高效地处理多页爬取。它使用请求链机制在不同页面间导航并抓取详细数据。
使用类型化对象在IronWebScraper中有什么好处?
使用类型化对象,例如Movie类,可以改善代码组织、增强IntelliSense支持,并在从网站抓取数据时进行编译时类型检查。
如何组织从电影网站抓取的数据?
抓取的数据可以使用IronWebScraper的Scrape方法组织成JSON格式,这有助于以可管理和可分析的格式保存结构化数据。
`MetaData`功能在IronWebScraper中有什么作用?
IronWebScraper的MetaData功能帮助在请求之间保持对象状态,使数据可以在多个解析功能之间传递,这在多页抓取中非常有用。
为什么选择IronWebScraper进行数据提取?
IronWebScraper提供了一个可靠的框架,用于有效且高效的网页抓取,具备多页抓取、类型化数据对象和详细API参考等功能,非常适合复杂的数据收集项目。
IronWebScraper如何改善输出文件的管理?
IronWebScraper通过使用工作目录来组织所有抓取操作的输出文件,位于一个单一、预定义的位置,方便处理大规模抓取项目。
IronWebScraper还可以从详细电影页面中提取哪些附加数据?
IronWebScraper可以使用CSS选择器和自定义抓取功能,从详细的电影页面中提取附加数据,如电影描述、类型和演员,以获得全面的数据。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。




