C#とIronWebscraperを使用したオンライン映画サイトのスクレイピング
IronWebScraperは、HTML要素を解析して構造化データの保存用に型付きオブジェクトを作成し、メタデータを使用してページ間を移動することで、ウェブサイトから映画データを抽出し、包括的な映画情報データセットを構築します。 この C# Web スクラパー ライブラリを使用すると、構造化されていない Web コンテンツを、整理され分析可能なデータへと簡単に変換できます。
クイックスタート: C#で映画情報をスクレイピングする
- NuGet パッケージ マネージャーを使用して
IronWebScraperをインストールします - `` を継承するクラスを作成してください。
- `` をオーバーライドして、ライセンスを設定し、ターゲット URL をリクエストします
- `` をオーバーライドして、CSSセレクタを使用して映画データを抽出する
- `` メソッドを使用して、データを JSON 形式で保存します
-
IronWebScraper をNuGetパッケージマネージャでインストール
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 を使い始めましょう無料トライアル
Movie Scraper クラスを設定するにはどうすればよいですか?
実際のWebサイトの例から始めてください。 C# による Web スクレイピングのチュートリアルで解説した手法を用いて、映画サイトをスクレイピングします。
新しいクラスを追加し、その名前を `` とします:
専用のスクレイパークラスを作成することで、コードを整理し、再利用可能にすることができます。 このアプローチはオブジェクト指向の原則に従っており、後々機能を容易に拡張できるようになっています。
対象となるウェブサイトの構造はどのようなものですか?
スクレイピングのためにサイトの構造を調査する。 効果的なウェブスクレイピングを行うには、ウェブサイトの構造を理解することが不可欠です。 "オンライン映画サイトからのスクレイピング"ガイドと同様に、まず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>
映画の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 div
End Sub
End Class
Working Directory プロパティの用途は何ですか?
このコードの新機能は何ですか?
Working Directory プロパティは、スクレイピングされたすべてのデータおよび関連ファイルのメイン作業ディレクトリを設定します。 これにより、すべての出力ファイルが一箇所に整理され、大規模なスクレイピングプロジェクトの管理が容易になります。 ディレクトリが存在しない場合は、自動的に作成されます。
CSSセレクタと属性は、それぞれどのような場合に使用すべきですか?
その他の留意点:
CSSセレクタは、要素の構造上の位置やクラス名に基づいて要素を指定する場合に最適ですが、IDやカスタムデータ属性などの特定の値を抽出するには、属性への直接アクセスの方が適しています。 この例では、CSSセレクタ(#movie-featured > div)を使用して特定の値を抽出しています。
スクレイピングしたデータに対して型付きオブジェクトを作成するにはどうすればよいですか?
スクレイピングしたデータをフォーマットされたオブジェクトとして保持するために、型付きオブジェクトを構築します。 強タイプ化されたオブジェクトを使用することで、コードの整理整頓、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; }
}
Public Class Movie
Public Property Id As Integer
Public Property Title As String
Public Property URL As String
End Class
型付きオブジェクトを使用することで、データの整理はどのように改善されるのでしょうか?
汎用的な ディクショナリの代わりに、型付き クラスを使用するようにコードを更新してください:
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(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 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 div
End Sub
End Class
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>
Movie クラスにプロパティを追加するにはどうすればよいですか?
クラスを、新しいプロパティ(, ,) ただし、、およびのみを使用します。
using System.Co/llections.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.Co/llections.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 Class
スクレイピング中にページ間を移動するにはどうすればよいですか?
詳細ページに移動して、そのページをスクレイピングしてください。 IronWebscraperはスレッドセーフ性を自動的に処理するため、複数のページを同時に処理することが可能です。
ページの種類ごとに複数のパース関数を使用する理由とは?
IronWebScraper を使用すると、さまざまなページ形式に対応するために複数のスクレイピング関数を追加できます。 この関心の分離により、コードの保守性が向上し、さまざまなページ構造に適切に対応できるようになります。 各パース関数は、特定のページタイプからのデータ抽出に特化することができます。
メタデータは、パース関数間でオブジェクトを渡す際にどのように役立つのでしょうか?
MetaData 機能は、リクエスト間の状態を維持するために不可欠です。 より高度なWEBSCRAPER機能については、詳細ガイドをご覧ください:
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(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 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 div
End Sub
Public Sub ParseDetails(ByVal 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 genre
' 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 actor
' Scrape and store detailed movie data
Scrape(movie, "Movie.Jsonl")
End Sub
End Class
この複数ページスクレイピング手法の主な特徴は何ですか?
新機能
- ショッピングサイトからのスクレイピングで用いられる手法と同様に、詳細ページをスクレイピングするためのスクレイピング関数(例:``)を追加してください。
- ファイルを生成する `` 関数を新しい関数に移動し、すべての詳細が収集された後にのみデータが保存されるようにしてください。
- IronWebScraperの機能(``)を使用して、映画オブジェクトを新しいスクレイピング関数に渡すことで、リクエスト間でオブジェクトの状態を維持します。
- ページをスクレイピングし、ムービーオブジェクトのデータを完全な情報と共にファイルに保存します。
利用可能なメソッドやプロパティの詳細については、APIリファレンスを参照してください。 IronWebscraperは、Webサイトから構造化データを抽出するための堅牢なフレームワークを提供し、データ収集および分析プロジェクトに不可欠なツールとなっています。
よくある質問
C#を使ってHTMLから映画のタイトルを抽出するには?
IronWebscraperは、HTMLから映画のタイトルを抽出するためのCSSセレクタメソッドを提供します。タイトル要素をターゲットにするには、'.movie-item h2'のような適切なセレクタでresponse.Css()メソッドを使用し、クリーンテキストの値を取得するためにTextContentCleanプロパティにアクセスします。
複数のムービーページをナビゲートするベストな方法は?
IronWebScraperはRequest()メソッドを通してページナビゲーションを処理します。CSSセレクタを使ってページネーションリンクを抽出し、複数のページからデータをスクレイピングするためにそれぞれのURLでRequest()を呼び出し、包括的なムービーデータセットを自動的に構築することができます。
スクレイピングしたムービーデータを構造化フォーマットで保存するには?
IronWebScraperのScrape()メソッドを使ってJSON形式でデータを保存する。タイトル、URL、レーティングのようなムービーのプロパティを含む匿名オブジェクトまたは型付きクラスを作成し、それらをファイル名とともにScrape()に渡すと、自動的にシリアライズしてデータを保存します。
ムービー情報を抽出するには、どの CSS セレクタを使用すればよいですか?
IronWebscraperは標準的なCSSセレクタをサポートしています。映画サイトの場合、コンテナには'.movie-item'、タイトルには'h2'、リンクには'a[href]'、レーティングやジャンルには特定のクラス名などのセレクタを使用します。Css()メソッドは、繰り返し処理できるコレクションを返します。
スクレイピングされたデータの「CAM」のようなムービーの品質指標はどのように扱えばよいですか?
IronWebScraperは、特定のHTML要素をターゲットとして品質指標を抽出し、処理することができます。CSSセレクタを使って品質バッジやテキストを見つけ、それらをスクレイピングされたデータオブジェクトのプロパティとして含めることで、包括的なムービー情報を得ることができます。
ムービーのスクレイピング操作にロギングを設定できますか?
はい、IronWebscraperはロギング機能を内蔵しています。Init()メソッドでLoggingLevelプロパティをLogLevel.Allに設定することで、すべてのスクレイピングアクティビティ、エラー、進捗状況を追跡し、ムービーデータ抽出のデバッグや監視に役立ちます。
スクレイピングされたデータの作業ディレクトリを設定する適切な方法は?
IronWebScraperでは、Init()メソッドでWorkingDirectoryプロパティを設定できます。スクレイピングされたムービーデータファイルが保存される「C# ∕MovieData∕Output∕」のようなパスを指定する。これにより、出力を一元管理し、データを整理することができる。
WebScraper クラスを正しく継承するにはどうすればよいですか?
IronWebscraperのWebScraper基底クラスを継承した新しいクラスを作成します。設定のためのInit()メソッドとデータ抽出ロジックのためのParse()メソッドをオーバーライドします。このオブジェクト指向のアプローチによって、あなたのムービースクレーパーは再利用可能で保守しやすくなります。
![IronScraperSample プロジェクトの C# テンプレート オプションが表示された Visual Studio の [新しい項目の追加] ダイアログ](/img/tutorials/webscraping-in-c-sharp/MovieScrapaerAddClass.jpg)





