Advertisement
dereksir

Untitled

Nov 27th, 2023 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using AngleSharp;
  2.  
  3. class Scraper
  4. {
  5.     static async Task Main()
  6.     {
  7.         string url = "https://scrapeme.live/shop/";
  8.  
  9.         // Create a new HttpClient instance
  10.         using (HttpClient client = new HttpClient())
  11.         {
  12.             // Send an HTTP GET request to the specified URL
  13.             HttpResponseMessage response = await client.GetAsync(url);
  14.  
  15.             // Read the HTML content from the response
  16.             string htmlContent = await response.Content.ReadAsStringAsync();
  17.  
  18.             // Configure AngleSharp
  19.             var config = Configuration.Default.WithDefaultLoader();
  20.             // Create a new browsing context
  21.             var context = BrowsingContext.New(config);
  22.             // Load HTML into AngleSharp
  23.             var document = await context.OpenAsync(req => req.Content(htmlContent));
  24.  
  25.             // Query all li within the <ul> with class 'products'
  26.             var Pokemons = document.QuerySelectorAll(".products li");
  27.  
  28.             // Check if any lists exist
  29.             if (Pokemons != null)
  30.             {
  31.                 // Iterate through each li
  32.                 foreach (var pokemon in Pokemons)
  33.                 {
  34.                     // Extract Pokémon information: name, price, and image
  35.                     string name = pokemon.QuerySelector("h2")?.TextContent ?? "Name not available";
  36.                     string price = pokemon.QuerySelector(".price")?.TextContent ?? "Price not available";
  37.                     string image = pokemon.QuerySelector("img")?.GetAttribute("src") ?? "Image not available";
  38.  
  39.                     Console.WriteLine($"Pokémon Name: {name}");
  40.                     Console.WriteLine($"Pokémon Price: {price}");
  41.                     Console.WriteLine($"Pokémon Image: {image}");
  42.                     Console.WriteLine(new string('-', 30)); // Separator for better readability
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement