C#과 IronWebScraper를 사용하여 온라인 영화 웹사이트 스크래핑하기

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronWebScraper는 HTML 요소를 파싱하고, 구조화된 데이터 저장을 위한 타입화된 객체를 생성하며, 메타데이터를 사용하여 페이지 간을 이동함으로써 웹사이트에서 영화 데이터를 추출하여 포괄적인 영화 정보 데이터 세트를 구축합니다. 이 C# 웹 스크레이퍼 라이브러리는 비정형 웹 콘텐츠를 체계적이고 분석 가능한 데이터로 변환하는 과정을 간소화합니다.

빠른 시작: C#으로 영화 정보 스크래핑하기

  1. NuGet 패키지 관리자를 통해 IronWebScraper를 설치합니다.
  2. ``를 상속하는 클래스를 생성하십시오.
  3. ``을 재정의하여 라이선스를 설정하고 대상 URL을 요청합니다.
  4. ``를 오버라이드하여 CSS 선택자를 사용하여 영화 데이터를 추출합니다.
  5. `` 메서드를 사용하여 데이터를 JSON 형식으로 저장합니다.
  1. NuGet 패키지 관리자를 사용하여 https://www.nuget.org/packages/IronWebScraper 설치하기

    PM > Install-Package IronWebScraper
  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();
  3. 실제 운영 환경에서 테스트할 수 있도록 배포하세요.

    무료 체험판으로 오늘 프로젝트에서 IronWebScraper 사용 시작하기

    arrow pointer

Movie Scraper 클래스는 어떻게 설정하나요?

실제 웹사이트 예시를 통해 시작하십시오. C# 웹 스크래핑 튜토리얼에 설명된 기법을 사용하여 영화 웹사이트를 스크래핑해 보겠습니다.

새 클래스를 추가하고 이름을 ``로 지정하십시오:

IronScraperSample 프로젝트의 C# 템플릿 옵션을 보여주는 Visual Studio '새 항목 추가' 대화 상자

전용 스크레이퍼 클래스를 생성하면 코드를 체계적으로 정리하고 재사용할 수 있습니다. 이 접근 방식은 객체 지향 원칙을 따르며, 추후 기능을 쉽게 확장할 수 있도록 합니다.

대상 웹사이트의 구조는 어떻게 되나요?

스크래핑을 위해 사이트 구조를 검토하십시오. 효과적인 웹 스크래핑을 위해서는 웹사이트의 구조를 이해하는 것이 매우 중요합니다. '온라인 영화 웹사이트에서 데이터 추출하기' 가이드와 마찬가지로, 먼저 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, 제목 및 상세 페이지 링크가 있습니다. 각 영화는 요소 내에 포함되어 있으며, 클래스를 가지고 있고 식별을 위한 고유한 `` 속성을 포함합니다.

기본적인 동영상 스크래핑은 어떻게 구현하나요?

이 데이터 세트의 스크래핑을 시작하세요. 스크레이퍼를 실행하기 전에 아래와 같이 라이선스 키가 올바르게 설정되었는지 확인하십시오:

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
$vbLabelText   $csharpLabel

Working Directory 속성은 무엇에 사용됩니까?

이 코드의 새로운 점은 무엇인가요?

Working Directory 속성은 스크래핑된 모든 데이터와 관련 파일의 기본 작업 디렉터리를 설정합니다. 이를 통해 모든 출력 파일이 한 곳에 체계적으로 정리되어 대규모 스크래핑 프로젝트를 보다 쉽게 관리할 수 있습니다. 해당 디렉터리가 존재하지 않으면 자동으로 생성됩니다.

CSS 선택자와 속성은 언제 각각 사용해야 할까요?

추가 고려 사항:

CSS 선택자는 요소의 구조적 위치나 클래스 이름을 기준으로 요소를 지정할 때 적합하며, ID나 사용자 정의 데이터 속성과 같은 특정 값을 추출할 때는 속성에 직접 접근하는 방식이 더 효과적입니다. 이 예시에서는 CSS 선택자(#movie-featured &gt; div)를 사용하여 DOM 구조를 탐색하고, 속성(``)을 통해 특정 값을 추출합니다.

스크래핑된 데이터에 대해 타입 지정된 객체를 어떻게 생성하나요?

스크래핑된 데이터를 형식화된 객체에 저장하기 위해 타입화된 객체를 생성합니다. 강타입 객체를 사용하면 코드 구성이 개선되고, 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
$vbLabelText   $csharpLabel

유형 지정된 객체를 사용하면 데이터 구성이 어떻게 개선되나요?

일반적인 사전 대신 타입이 지정된 클래스를 사용하도록 코드를 업데이트하십시오:

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
$vbLabelText   $csharpLabel

Scrape 메서드는 타입화된 객체에 대해 어떤 형식을 사용합니까?

새로운 소식은 무엇인가요?

  1. 스크래핑된 데이터를 저장하기 위해 `` 클래스를 구현하여 타입 안전성과 더 나은 코드 구성을 제공했습니다.
  2. 영화 객체를 `` 메서드에 전달하면, 이 메서드는 해당 형식을 인식하여 아래와 같이 정의된 방식으로 저장합니다:

제목, URL 및 메타데이터 필드를 포함한 구조화된 영화 데이터가 담긴 JSON 영화 데이터베이스를 보여주는 메모장 창

출력 결과는 자동으로 JSON 형식으로 시리얼화되어 데이터베이스나 다른 애플리케이션으로 쉽게 가져올 수 있습니다.

상세 영화 페이지를 어떻게 스크래핑하나요?

더 상세한 페이지 스크래핑을 시작하세요. 여러 페이지에 걸친 스크래핑은 흔히 요구되는 기능이며, IronWebScraper는 요청 연쇄(request chaining) 메커니즘을 통해 이를 간편하게 처리합니다.

상세 페이지에서 어떤 추가 데이터를 추출할 수 있나요?

영화 페이지는 다음과 같으며, 각 영화에 대한 풍부한 메타데이터를 포함하고 있습니다:

포스터와 출연진, 평점 등 영화 정보를 포함한 '가디언즈 오브 갤럭시 Vol. 2' 영화 정보 페이지

<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

Movie 클래스에 추가 속성을 확장하려면 어떻게 해야 합니까?

클래스를 새로운 속성(, ,, ,, 상영 시간, IMDb 평점) 단, 만 사용설명, , 및만 사용하십시오. string>@@--CODEstring>-243--@@Liststring>@@--COstring>DE-244--@@를 장르와 배우에 적용하면 여러 값을 우아하게 처리할 수 있습니다:

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
$vbLabelText   $csharpLabel

스크래핑 중 페이지 간 이동은 어떻게 하나요?

상세 페이지로 이동하여 스크래핑하십시오. 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
$vbLabelText   $csharpLabel

이 다중 페이지 스크래핑 방식의 주요 기능은 무엇입니까?

새로운 소식은 무엇인가요?

  1. 쇼핑 웹사이트에서 데이터를 추출할 때 사용하는 기법과 유사하게, 상세 페이지를 스크래핑하기 위한 스크래핑 함수(예: ``)를 추가하십시오.
  2. 파일을 생성하는 `` 함수를 새로운 함수로 이동하여, 모든 세부 정보가 수집된 후에만 데이터가 저장되도록 하십시오.
  3. IronWebScraper 기능(``)을 사용하여 영화 객체를 새로운 스크래핑 함수로 전달하고, 요청 간에 객체 상태를 유지합니다.
  4. 페이지를 스크래핑하고 영화 객체 데이터를 완전한 정보와 함께 파일에 저장합니다.

영화 제목, 설명, URL 및 장르 메타데이터가 포함된 JSON 영화 데이터베이스를 보여주는 메모장 창

using 가능한 메서드 및 속성에 대한 자세한 내용은 API 레퍼런스를 참조하십시오. IronWebScraper는 웹사이트에서 구조화된 데이터를 추출하기 위한 강력한 프레임워크를 제공하여, 데이터 수집 및 분석 프로젝트에 필수적인 도구입니다.

자주 묻는 질문

C#을 사용하여 HTML에서 영화 제목을 어떻게 추출할 수 있습니까?

IronWebScraper는 HTML에서 영화 제목을 추출하기 위한 CSS 선택자 메서드를 제공합니다. 적절한 선택자('.movie-item h2')를 사용하여 제목 요소를 타겟하고, 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() 메서드를 재정의하십시오. 이 객체 지향적 접근법은 영화 스크래퍼를 재사용 가능하고 유지 관리 가능하게 만듭니다.

커티스 차우
기술 문서 작성자

커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.

커티스는 개발 분야 외에도 사물 인터넷(IoT)에 깊은 관심을 가지고 있으며, 하드웨어와 소프트웨어를 통합하는 혁신적인 방법을 연구합니다. 여가 시간에는 게임을 즐기거나 디스코드 봇을 만들면서 기술에 대한 애정과 창의성을 결합합니다.

시작할 준비 되셨나요?
Nuget 다운로드 137,906 | 버전: 2026.6 just released
Still Scrolling Icon

아직도 스크롤하고 계신가요?

빠른 증거를 원하시나요? PM > Install-Package IronWebScraper
샘플 실행 대상 사이트가 구조화된 데이터로 변환됩니다.