Advertisement
dereksir

Untitled

Nov 27th, 2023 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 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 new HttpClient instance
  10.         using (HttpClient client = new HttpClient())
  11.         {
  12.             // Sending a GET request to the specified URL
  13.             HttpResponseMessage response = await client.GetAsync(url);
  14.  
  15.             // Reading the HTML content from the response
  16.             string htmlContent = await response.Content.ReadAsStringAsync();
  17.  
  18.             // Configuring AngleSharp
  19.             var config = Configuration.Default.WithDefaultLoader();
  20.             // Creating a new browsing context
  21.             var context = BrowsingContext.New(config);
  22.             // Loading the fetched HTML content into AngleSharp.
  23.             var document = await context.OpenAsync(req => req.Content(htmlContent));
  24.  
  25.             // Querying for the first Pokémon element
  26.             var firstPokemon = document.QuerySelector(".post-759");
  27.  
  28.             // Checking if a Pokémon element is found
  29.             if (firstPokemon != null)
  30.             {
  31.                 // Extracting Pokémon information: name, price, and image
  32.                 string name = firstPokemon.QuerySelector("h2")?.TextContent ?? "Not available";
  33.                 string price = firstPokemon.QuerySelector(".price")?.TextContent ?? "Not available";
  34.                 string image = firstPokemon.QuerySelector("img")?.GetAttribute("src") ?? "Not available";
  35.  
  36.                 Console.WriteLine($"Pokémon Name: {name}");
  37.                 Console.WriteLine($"Pokémon Price: {price}");
  38.                 Console.WriteLine($"Pokémon Image: {image}");
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement