web scrape in csharp

To perform web scraping in C#, we can use the HTMLAgilityPack library and the System.Net namespace.

Here's a simple example of how to scrape a website using C#:

main.cs
using System;
using System.Net;
using HtmlAgilityPack;

namespace WebScraper
{
    class Program
    {
        static void Main(string[] args)
        {
            // Make a request to the website
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            
            // Get the HTML content
            string htmlContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
            
            // Load the HTML content into a document for parsing
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(htmlContent);
            
            // Select the elements you want to scrape and loop through them
            foreach (HtmlNode node in document.DocumentNode.SelectNodes("//div[@class='example']"))
            {
                // Get the inner text of the element
                string innerText = node.InnerText;
                Console.WriteLine(innerText);
            }
        }
    }
}
1081 chars
32 lines

Here, we first create an instance of HttpWebRequest to make a request to the website. We then get the HTML content as a string and load it into an instance of HtmlDocument. We can then use SelectNodes to select the elements we want to scrape and loop through them. Finally, we use the InnerText property to get the text inside the element.

gistlibby LogSnag