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 new HttpClient instance
- using (HttpClient client = new HttpClient())
- {
- // Sending a GET request to the specified URL
- HttpResponseMessage response = await client.GetAsync(url);
- // Reading the HTML content from the response
- string htmlContent = await response.Content.ReadAsStringAsync();
- // Configuring AngleSharp
- var config = Configuration.Default.WithDefaultLoader();
- // Creating a new browsing context
- var context = BrowsingContext.New(config);
- // Loading the fetched HTML content into AngleSharp.
- var document = await context.OpenAsync(req => req.Content(htmlContent));
- // Querying for the first Pokémon element
- var firstPokemon = document.QuerySelector(".post-759");
- // Checking if a Pokémon element is found
- if (firstPokemon != null)
- {
- // Extracting Pokémon information: name, price, and image
- string name = firstPokemon.QuerySelector("h2")?.TextContent ?? "Not available";
- string price = firstPokemon.QuerySelector(".price")?.TextContent ?? "Not available";
- string image = firstPokemon.QuerySelector("img")?.GetAttribute("src") ?? "Not available";
- Console.WriteLine($"Pokémon Name: {name}");
- Console.WriteLine($"Pokémon Price: {price}");
- Console.WriteLine($"Pokémon Image: {image}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement