Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AngleSharp;
- class Scraper
- {
- static async Task Main()
- {
- string url = "https://scrapeme.live/shop/";
- // Create a new HttpClient instance
- using (HttpClient client = new HttpClient())
- {
- // Send an HTTP GET request to the specified URL
- HttpResponseMessage response = await client.GetAsync(url);
- // Read the HTML content from the response
- string htmlContent = await response.Content.ReadAsStringAsync();
- // Configure AngleSharp
- var config = Configuration.Default.WithDefaultLoader();
- // Create a new browsing context
- var context = BrowsingContext.New(config);
- // Load HTML into AngleSharp
- var document = await context.OpenAsync(req => req.Content(htmlContent));
- // Query all li within the <ul> with class 'products'
- var Pokemons = document.QuerySelectorAll(".products li");
- // Check if any lists exist
- if (Pokemons != null)
- {
- // Iterate through each li
- foreach (var pokemon in Pokemons)
- {
- // Extract Pokémon information: name, price, and image
- string name = pokemon.QuerySelector("h2")?.TextContent ?? "Name not available";
- string price = pokemon.QuerySelector(".price")?.TextContent ?? "Price not available";
- string image = pokemon.QuerySelector("img")?.GetAttribute("src") ?? "Image not available";
- Console.WriteLine($"Pokémon Name: {name}");
- Console.WriteLine($"Pokémon Price: {price}");
- Console.WriteLine($"Pokémon Image: {image}");
- Console.WriteLine(new string('-', 30)); // Separator for better readability
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement