C#でショッピングサイトをスクレイピングする
WebScraperフレームワークを使用してC#を使用してショッピングウェブサイトから商品カテゴリとアイテムをスクレイピングし、HTML要素からカスタムモデルに構造化データを抽出する方法を学びます。 この包括的なガイドでは、IronWebScraperライブラリを使用して堅牢なeコマーススクレーパーを構築する手順を説明します。
クイックスタート: C#でショッピングサイトをスクレイピング
今すぐ NuGet で PDF を作成してみましょう:
NuGet パッケージ マネージャーを使用して IronWebScraper をインストールします
このコード スニペットをコピーして実行します。
using IronWebScraper; public class QuickShoppingScraper : WebScraper { public override void Init() { // Apply your license key License.LicenseKey = "YOUR-LICENSE-KEY"; // Set the starting URL this.Request("https://shopping-site.com", Parse); } public override void Parse(Response response) { // Extract product data foreach (var product in response.Css(".product-item")) { var item = new { Name = product.Css(".product-name").First().InnerText, Price = product.Css(".price").First().InnerText, Image = product.Css("img").First().Attributes["src"] }; Scrape(item, "products.jsonl"); } } } // Run the scraper var scraper = new QuickShoppingScraper(); scraper.Start();実際の環境でテストするためにデプロイする
1."ShoppingSiteSample "という名前の新しいコンソールアプリプロジェクトを作成してください。 2.WebScraperを継承する "ShoppingScraper "というクラスを追加する。 3.カテゴリと商品データのモデルを作成する。 4.Init()をオーバーライドして開始URLとスクレイピング用のParse()メソッドを設定する。 5.スクレーパーを実行し、カテゴリーと商品をJSONLファイルに抽出する。
ショッピングサイトのHTML構造を分析するにはどうすればよいですか?
ショッピングサイトを選び、そのコンテンツ構造を分析します。 ウェブスクレイピングを成功させるには、HTML構造を理解することが重要です。 コードを記述する前に、ブラウザの開発者ツールを使用して、ターゲットとなるウェブサイトの構造を分析することに時間を費やしてください。
画像に示すように、左サイドバーには、サイトの製品カテゴリへのリンクが含まれています。 最初のステップは、サイトのHTMLを調査し、スクレイピングのアプローチを計画することです。 この分析段階は、効果的なスクレイピング戦略を構築するために不可欠です。
なぜ HTML 構造を理解することが重要なのですか?
ファッションサイトにはサブカテゴリ(男性、女性、子供)が含まれています。 この階層構造を理解することは、適切なデータモデルとスクレイピングロジックの設計に役立ちます。 高度なウェブスクレイピング機能を使用する場合は、適切なHTML解析がさらに重要になります。
<li class="menu-item" data-id="">
<a href="https://domain.com/fashion-by-/" class="main-category">
<i class="cat-icon osh-font-fashion"></i>
<span class="nav-subTxt">FASHION </span>
<i class="osh-font-light-arrow-left"></i><i class="osh-font-light-arrow-right"></i>
</a>
<div class="navLayerWrapper" style="width: 633px; display: none;">
<div class="submenu">
<div class="column">
<div class="categories">
<a class="category" href="https://domain.com/fashion-by-/?sort=newest&dir=desc&viewType=gridView3">New Arrivals !</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/men-fashion/">Men</a>
<a class="subcategory" href="https://domain.com/mens-shoes/">Shoes</a>
<a class="subcategory" href="https://domain.com/mens-clothing/">Clothing</a>
<a class="subcategory" href="https://domain.com/mens-accessories/">Accessories</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/women-fashion/">Women</a>
<a class="subcategory" href="https://domain.com/womens-shoes/">Shoes</a>
<a class="subcategory" href="https://domain.com/womens-clothing/">Clothing</a>
<a class="subcategory" href="https://domain.com/womens-accessories/">Accessories</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/girls-boys-fashion/">Kids</a>
<a class="subcategory" href="https://domain.com/boys-fashion/">Boys</a>
<a class="subcategory" href="https://domain.com/girls/">Girls</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/maternity-clothes/">Maternity Clothes</a>
</div>
</div>
<div class="column">
<div class="categories">
<span class="category defaultCursor">Men Best Sellers</span>
<a class="subcategory" href="https://domain.com/mens-casual-shoes/">Casual Shoes</a>
<a class="subcategory" href="https://domain.com/mens-sneakers/">Sneakers</a>
<a class="subcategory" href="https://domain.com/mens-t-shirts/">T-shirts</a>
<a class="subcategory" href="https://domain.com/mens-polos/">Polos</a>
</div>
<div class="categories">
<span class="category defaultCursor">Women Best Sellers</span>
<a class="subcategory" href="https://domain.com/womens-sandals/">Sandals</a>
<a class="subcategory" href="https://domain.com/womens-sneakers/">Sneakers</a>
<a class="subcategory" href="https://domain.com/women-dresses/">Dresses</a>
<a class="subcategory" href="https://domain.com/women-tops/">Tops</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/womens-curvy-clothing/">Women's Curvy Clothing</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/fashion-bundles/v/">Fashion Bundles</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/hijab-fashion/">Hijab Fashion</a>
</div>
</div>
<div class="column">
<div class="categories">
<a class="category" href="https://domain.com/brands/fashion-by-/">SEE ALL BRANDS</a>
<a class="subcategory" href="https://domain.com/adidas/">Adidas</a>
<a class="subcategory" href="https://domain.com/converse/">Converse</a>
<a class="subcategory" href="https://domain.com/ravin/">Ravin</a>
<a class="subcategory" href="https://domain.com/dejavu/">Dejavu</a>
<a class="subcategory" href="https://domain.com/agu/">Agu</a>
<a class="subcategory" href="https://domain.com/activ/">Activ</a>
<a class="subcategory" href="https://domain.com/oxford--bellini--tie-house--milano/">Tie House</a>
<a class="subcategory" href="https://domain.com/shoe-room/">Shoe Room</a>
<a class="subcategory" href="https://domain.com/town-team/">Town Team</a>
</div>
</div>
</div>
</div>
</li><li class="menu-item" data-id="">
<a href="https://domain.com/fashion-by-/" class="main-category">
<i class="cat-icon osh-font-fashion"></i>
<span class="nav-subTxt">FASHION </span>
<i class="osh-font-light-arrow-left"></i><i class="osh-font-light-arrow-right"></i>
</a>
<div class="navLayerWrapper" style="width: 633px; display: none;">
<div class="submenu">
<div class="column">
<div class="categories">
<a class="category" href="https://domain.com/fashion-by-/?sort=newest&dir=desc&viewType=gridView3">New Arrivals !</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/men-fashion/">Men</a>
<a class="subcategory" href="https://domain.com/mens-shoes/">Shoes</a>
<a class="subcategory" href="https://domain.com/mens-clothing/">Clothing</a>
<a class="subcategory" href="https://domain.com/mens-accessories/">Accessories</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/women-fashion/">Women</a>
<a class="subcategory" href="https://domain.com/womens-shoes/">Shoes</a>
<a class="subcategory" href="https://domain.com/womens-clothing/">Clothing</a>
<a class="subcategory" href="https://domain.com/womens-accessories/">Accessories</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/girls-boys-fashion/">Kids</a>
<a class="subcategory" href="https://domain.com/boys-fashion/">Boys</a>
<a class="subcategory" href="https://domain.com/girls/">Girls</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/maternity-clothes/">Maternity Clothes</a>
</div>
</div>
<div class="column">
<div class="categories">
<span class="category defaultCursor">Men Best Sellers</span>
<a class="subcategory" href="https://domain.com/mens-casual-shoes/">Casual Shoes</a>
<a class="subcategory" href="https://domain.com/mens-sneakers/">Sneakers</a>
<a class="subcategory" href="https://domain.com/mens-t-shirts/">T-shirts</a>
<a class="subcategory" href="https://domain.com/mens-polos/">Polos</a>
</div>
<div class="categories">
<span class="category defaultCursor">Women Best Sellers</span>
<a class="subcategory" href="https://domain.com/womens-sandals/">Sandals</a>
<a class="subcategory" href="https://domain.com/womens-sneakers/">Sneakers</a>
<a class="subcategory" href="https://domain.com/women-dresses/">Dresses</a>
<a class="subcategory" href="https://domain.com/women-tops/">Tops</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/womens-curvy-clothing/">Women's Curvy Clothing</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/fashion-bundles/v/">Fashion Bundles</a>
</div>
<div class="categories">
<a class="category" href="https://domain.com/hijab-fashion/">Hijab Fashion</a>
</div>
</div>
<div class="column">
<div class="categories">
<a class="category" href="https://domain.com/brands/fashion-by-/">SEE ALL BRANDS</a>
<a class="subcategory" href="https://domain.com/adidas/">Adidas</a>
<a class="subcategory" href="https://domain.com/converse/">Converse</a>
<a class="subcategory" href="https://domain.com/ravin/">Ravin</a>
<a class="subcategory" href="https://domain.com/dejavu/">Dejavu</a>
<a class="subcategory" href="https://domain.com/agu/">Agu</a>
<a class="subcategory" href="https://domain.com/activ/">Activ</a>
<a class="subcategory" href="https://domain.com/oxford--bellini--tie-house--milano/">Tie House</a>
<a class="subcategory" href="https://domain.com/shoe-room/">Shoe Room</a>
<a class="subcategory" href="https://domain.com/town-team/">Town Team</a>
</div>
</div>
</div>
</div>
</li>どのようにウェブスクレイピングプロジェクトをセットアップしますか?
C#ウェブスクレイピングのベストプラクティスに従ってプロジェクトを立ち上げてください。
1.新しいコンソールアプリを作成するか、"ShoppingSiteSample "という名前のサンプル用の新しいフォルダを追加してください。 2."ShoppingScraper "という新しいクラスを追加してください。 3.サイトのカテゴリとそのサブカテゴリをスクレイピングすることから始めます。 4.NuGetパッケージマネージャまたはパッケージマネージャコンソールからIronWebscraperをインストールしてください:
Install-Package IronWebScraperInstall-Package IronWebScraperIRON VB CONVERTER ERROR developers@ironsoftware.comカテゴリにはどのようなデータモデルを使うべきですか?
発見された階層構造を適切に表現するカテゴリーモデルを作成すること:
public class Category
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the URL.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string URL { get; set; }
/// <summary>
/// Gets or sets the subcategories.
/// </summary>
/// <value>
/// The subcategories.
/// </value>
public List<Category> SubCategories { get; set; }
// Additional properties for enhanced data collection
public int ProductCount { get; set; }
public DateTime LastScraped { get; set; }
public string CategoryType { get; set; }
}public class Category
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the URL.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string URL { get; set; }
/// <summary>
/// Gets or sets the subcategories.
/// </summary>
/// <value>
/// The subcategories.
/// </value>
public List<Category> SubCategories { get; set; }
// Additional properties for enhanced data collection
public int ProductCount { get; set; }
public DateTime LastScraped { get; set; }
public string CategoryType { get; set; }
}IRON VB CONVERTER ERROR developers@ironsoftware.com基本的なスクレーパーのロジックはどのように構築しますか?
スクレイパーを実行する前に、ライセンスキーを適用することを忘れずに、スクレイパーのロジックを構築してください:
public class ShoppingScraper : WebScraper
{
/// <summary>
/// Initialize the web scraper, setting the start URLs and allowed/banned domains or URL patterns.
/// </summary>
public override void Init()
{
// Apply your license key - get one from https://ironsoftware.com/csharp/webscraper/licensing/
License.LicenseKey = "LicenseKey";
this.LoggingLevel = WebScraper.LogLevel.All;
this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";
// Configure request settings for better performance
this.Request("www.webSite.com", Parse);
}
/// <summary>
/// Parses the HTML document of the response to scrap the necessary data.
/// </summary>
/// <param name="response">The HTTP Response object to parse.</param>
public override void Parse(Response response)
{
var categoryList = new List<Category>();
// Iterate through each link in the menu and extract the category data.
foreach (var Links in response.Css("#menuFixed > ul > li > a"))
{
var cat = new Category
{
URL = Links.Attributes["href"],
Name = Links.InnerText,
LastScraped = DateTime.Now
};
categoryList.Add(cat);
}
// Save the scraped data into a JSONL file.
Scrape(categoryList, "Shopping.jsonl");
}
}public class ShoppingScraper : WebScraper
{
/// <summary>
/// Initialize the web scraper, setting the start URLs and allowed/banned domains or URL patterns.
/// </summary>
public override void Init()
{
// Apply your license key - get one from https://ironsoftware.com/csharp/webscraper/licensing/
License.LicenseKey = "LicenseKey";
this.LoggingLevel = WebScraper.LogLevel.All;
this.WorkingDirectory = AppSetting.GetAppRoot() + @"\ShoppingSiteSample\Output\";
// Configure request settings for better performance
this.Request("www.webSite.com", Parse);
}
/// <summary>
/// Parses the HTML document of the response to scrap the necessary data.
/// </summary>
/// <param name="response">The HTTP Response object to parse.</param>
public override void Parse(Response response)
{
var categoryList = new List<Category>();
// Iterate through each link in the menu and extract the category data.
foreach (var Links in response.Css("#menuFixed > ul > li > a"))
{
var cat = new Category
{
URL = Links.Attributes["href"],
Name = Links.InnerText,
LastScraped = DateTime.Now
};
categoryList.Add(cat);
}
// Save the scraped data into a JSONL file.
Scrape(categoryList, "Shopping.jsonl");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comメニューのどの要素をターゲットにしていますか?
メニューからリンクをスクレイピングするには、正確なCSSセレクタが必要です。 APIリファレンスは、利用可能なセレクタ・メソッドの詳細情報を提供します:
メインカテゴリとサブカテゴリの両方をスクレイピングするには? メインカテゴリとすべてのサブリンクをスクレイピングするようにコードを更新してください。 このアプローチにより、ナビゲーションの構造を完全に把握することができます: ```csharp public override void Parse(Response response) { // List of Category Links (Root) var categoryList = new List(); // Traverse each 'li' under the fixed menu foreach (var li in response.Css("#menuFixed > ul > li")) { // List of Main Links foreach (var Links in li.Css("a")) { var cat = new Category { URL = Links.Attributes["href"], Name = Links.InnerText, SubCategories = new List(), LastScraped = DateTime.Now }; // List of Subcategories Links foreach (var subCategory in li.Css("a[class=subcategory]")) { var subcat = new Category { URL = subCategory.Attributes["href"], Name = subCategory.InnerText, CategoryType = "Subcategory" }; // Check if subcategory link already exists if (cat.SubCategories.Find(c => c.Name == subcat.Name && c.URL == subcat.URL) == null) { // Add sublinks cat.SubCategories.Add(subcat); } } // Update product count based on subcategories cat.ProductCount = cat.SubCategories.Count; // Add Main Category to the list categoryList.Add(cat); } } // Save the scraped data into a JSONL file. Scrape(categoryList, "Shopping.jsonl"); } ``` ## カテゴリページから製品情報を抽出するにはどうすればよいですか? すべてのサイトカテゴリへのリンクが利用可能であるため、各カテゴリ内の製品のスクレイピングを開始します。 製品ページを扱う場合、[スレッドセーフ](https://ironsoftware.com/csharp/webscraper/troubleshooting/thread-safety/)が最適なパフォーマンスのために重要になります。 任意のカテゴリに移動し、内容を確認してください:### 製品の HTML 構造はどのようなものですか? 製品の構成を理解するためにHTMLの構造を調べます: ```html ``` ### どの製品モデルを作成すればよいですか? このコンテンツの製品モデルを構築してください。 [ショッピングウェブサイトのスクレイピング](https://ironsoftware.com/csharp/webscraper/how-to/scraping-from-a-shopping-website/)で作業する場合は、関連するすべての製品の詳細をキャプチャします: ```csharp public class Product { ////// Gets or sets the name. /// ////// The name. /// public string Name { get; set; } ////// Gets or sets the price. /// ////// The price. /// public string Price { get; set; } ////// Gets or sets the image. /// ////// The image. /// public string Image { get; set; } // Additional properties for comprehensive data collection public string Brand { get; set; } public string OldPrice { get; set; } public string Discount { get; set; } public float Rating { get; set; } public int ReviewCount { get; set; } public ListAvailableSizes { get; set; } public string ProductUrl { get; set; } public string SKU { get; set; } public DateTime ScrapedDate { get; set; } } ``` ### 商品スクレイピング機能を追加するにはどうすればよいですか? カテゴリページをスクレイピングするために、エラー処理とデータ検証を行う新しいスクレイピングメソッドを追加してください: ```csharp public void ParseCategory(Response response) { // List of Products var productList = new List(); // Iterate through product links in the product section foreach (var Links in response.Css("section.products > div > a")) { try { var product = new Product { Name = Links.Css("h2.title > span.name").First().InnerText, Brand = Links.Css("h2.title > span.brand").FirstOrDefault()?.InnerText ?? "Unknown", Price = Links.Css("div.price-container > span.price-box > span.price > span[data-price]").First().InnerText, Image = Links.Css("div.image-wrapper.default-state > img").First().Attributes["src"], ProductUrl = Links.Attributes["href"], SKU = Links.ParentNode.Attributes["data-sku"], ScrapedDate = DateTime.Now }; // Extract old price if available var oldPriceElement = Links.Css("span.price.-old > span[data-price]").FirstOrDefault(); if (oldPriceElement != null) { product.OldPrice = oldPriceElement.InnerText; } // Extract discount percentage var discountElement = Links.Css("span.sale-flag-percent").FirstOrDefault(); if (discountElement != null) { product.Discount = discountElement.InnerText; } // Extract rating information var ratingWidth = Links.Css("div.stars").FirstOrDefault()?.Attributes["style"]; if (!string.IsNullOrEmpty(ratingWidth)) { var width = System.Text.RegularExpressions.Regex.Match(ratingWidth, @"(\d+)%").Groups[1].Value; if (int.TryParse(width, out int ratingPercent)) { product.Rating = ratingPercent / 20.0f; // Convert percentage to 5-star scale } } // Extract review count var reviewText = Links.Css("div.total-ratings").FirstOrDefault()?.InnerText; if (!string.IsNullOrEmpty(reviewText)) { var reviewCount = System.Text.RegularExpressions.Regex.Match(reviewText, @"\d+").Value; if (int.TryParse(reviewCount, out int count)) { product.ReviewCount = count; } } // Extract available sizes product.AvailableSizes = Links.Css("div.list.-sizes > span.sku-size") .Select(s => s.InnerText) .ToList(); productList.Add(product); } catch (Exception ex) { // Log error and continue with next product Console.WriteLine($"Error parsing product: {ex.Message}"); } } // Save the scraped product data into a JSONL file. Scrape(productList, "Products.jsonl"); // Handle pagination if needed var nextPageLink = response.Css("a.pagination-next").FirstOrDefault(); if (nextPageLink != null) { var nextPageUrl = nextPageLink.Attributes["href"]; this.Request(nextPageUrl, ParseCategory); } } ``` ショッピングサイトをスクレイピングするためのこの包括的なアプローチは、エラーを優雅に処理しながら、関連するすべての製品情報を確実にキャプチャします。 より高度なシナリオについては、`IronWebscraper`で利用可能な[高度なウェブスクレイピング機能](https://ironsoftware.com/csharp/webscraper/tutorials/webscraping-in-c-sharp-advanced/)をご覧ください。
よくある質問
C#でショッピングサイトから商品データを抽出するには?
IronWebScraperは、CSSセレクタを使用することで、ショッピングサイトから商品データを簡単に抽出することができます。WebScraperクラスを作成し、Parseメソッドをオーバーライドし、response.Css()を使用して、商品名、価格、画像のような特定のHTML要素をターゲットにすることができます。抽出したデータは、JSON や JSONL ファイルなど、さまざまな形式に保存できます。
ショッピング・ウェブサイト・スクレーパーの基本的な作成手順は?
IronWebscraperでショッピング・ウェブサイト・スクレーパーを作成するには:1) Console Appプロジェクトを作成し、2) WebScraperを継承したクラスを追加し、3) カテゴリと商品のデータモデルを作成し、4) Init()メソッドをオーバーライドして開始URLを設定し、5) Parse()メソッドをオーバーライドしてCSSセレクタを使用してデータを抽出し、6) スクレーパーを実行してお好みのフォーマットにデータを保存します。
eコマースサイトをスクレイピングする際、階層的なカテゴリー構造をどのように扱えばよいですか?
IronWebScraperでは、親子関係(ファッション > メンズ > シューズのような)を反映した適切なデータモデルを作成することで、階層構造を扱うことができます。CSSセレクタを使ってネストされたHTML要素をナビゲートしたり、プログラム的にカテゴリーツリー構造を構築することができ、特にIronWebscraperの高度な機能を使うときに便利です。
スクレイピングの前にショッピングサイトの HTML 構造を分析する最善の方法は何ですか?
IronWebscraperを使ってショッピングサイトをスクレイピングする前に、ブラウザの開発者ツールを使ってHTMLの構造を調べてください。CSSのクラスや要素の階層に一貫したパターンがないか探してください。この分析は、IronWebscraperのParse()メソッドで使用する正しいCSSセレクタを特定するのに役立ちます。
同じページから商品リストとカテゴリーナビゲーションの両方を抽出できますか?
はい、IronWebScraperは1つのページから複数のタイプのデータを抽出することができます。Parse()メソッドの中で、異なるCSSセレクタを使ってカテゴリリンク('.category-item'のような)と商品リスト('.product-item'のような)を同時にターゲットにし、別々の出力ファイルやデータ構造に保存することができます。
スクレイピングした製品データをファイルに保存するにはどうすればよいですか?
IronWebScraperは、抽出されたデータを自動的に保存する組み込みのScrape()メソッドを提供します。データオブジェクトとファイル名をScrape(item, "products.jsonl")に渡すだけです。このライブラリはJSON、JSONL、CSVを含む様々な出力形式をサポートしており、スクレイピングされたeコマースデータをさらに処理するために簡単にエクスポートすることができます。












