Ever needed to pull data from websites automatically? Maybe you're tracking cryptocurrency prices, monitoring competitor pricing, or collecting product information for your business. That's where web scraping comes in handy, and C# makes it surprisingly straightforward.
Let me walk you through building a real-world web scraper that actually works. We'll extract cryptocurrency data from a live website and save it to a CSV file—no complex setup required.
Before we dive into code, here's the deal: manual data collection is tedious and time-consuming. A client recently needed to scrape product data from multiple supplier websites for their wholesale business. What would've taken days manually now runs automatically in minutes.
Web scraping automates this entire process. You send a request to a website, grab the HTML response, parse out the data you need, and store it however you want. Simple concept, powerful results.
Here's where things get interesting. ScraperAPI handles all the heavy lifting that typically makes web scraping a headache—rotating proxies, handling CAPTCHAs, managing retries, and dealing with bot detection systems.
Instead of wrestling with IP bans and anti-scraping measures, you simply send your target URL along with your API key. The service returns clean HTML data ready for parsing. When you're dealing with websites that have robust anti-bot protection, 👉 this proxy-based approach significantly improves scraping success rates and stability.
Getting started is painless. Sign up for an account and you'll receive 5,000 free API requests to test everything out. That's plenty to build and validate your scraper before committing to a paid plan.
Let's build something practical. We'll scrape cryptocurrency data from CoinMarketCap—names, prices, and market changes. This same approach works for any website once you understand the pattern.
Create a new Console App in .NET Core (or whatever project type fits your needs). The focus here is the scraping logic, so I'll skip the standard project creation steps.
You need two NuGet packages. Open your Package Manager Console and run these commands:
Install-Package ScraperApi
Install-Package HtmlAgilityPack
ScraperAPI is the official C# SDK that handles proxy rotation and anti-bot measures. HtmlAgilityPack parses HTML like a champ, letting you extract specific data using XPath queries.
Here's where the magic starts. This method fetches HTML data from your target website:
csharp
static async Task GetDataFromWebPage() {
string apiKey = "YOUR_API_KEY_HERE";
HttpClient scraperApiHttpClient = ScraperApiClient.GetProxyHttpClient(apiKey);
scraperApiHttpClient.BaseAddress = new Uri("https://coinmarketcap.com");
var response = await scraperApiHttpClient.GetAsync("/");
if (response.StatusCode == HttpStatusCode.OK) {
var htmlData = await response.Content.ReadAsStringAsync();
ParseHtml(htmlData);
}
}
Replace YOUR_API_KEY_HERE with your actual API key. The GetProxyHttpClient() method creates an HTTP client that routes requests through ScraperAPI's proxy network. When you call GetAsync(), it fetches the page and returns the HTML content.
For developers building scrapers that need to handle JavaScript-heavy sites or complex anti-bot systems, 👉 using a reliable scraping infrastructure can mean the difference between a project that works consistently and one that constantly breaks.
Once you have the HTML, you need to extract meaningful information. HTMLAgilityPack makes this straightforward:
csharp
static void ParseHtml(string htmlData) {
var coinData = new Dictionary<string, string>();
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlData);
var theHTML = htmlDoc.DocumentNode.SelectSingleNode("html//body");
var cmcTableBody = theHTML.SelectSingleNode("//tbody");
var cmcTableRows = cmcTableBody.SelectNodes("tr");
if (cmcTableRows != null) {
foreach(HtmlNode row in cmcTableRows) {
var cmcTableColumns = row.SelectNodes("td");
string name = cmcTableColumns[2].InnerText;
string price = cmcTableColumns[3].InnerText;
coinData.Add(name, price);
}
}
WriteDataToCSV(coinData);
}
This code loads the HTML into a document object, locates the table body containing cryptocurrency data, and loops through each row to extract the coin name and price. The SelectSingleNode() method grabs the first element matching your XPath query, while SelectNodes() returns a collection when you need multiple elements.
Finally, write your extracted data to a CSV file for easy access:
csharp
static void WriteDataToCSV(Dictionary<string, string> cryptoCurrencyData) {
var csvBuilder = new StringBuilder();
csvBuilder.AppendLine("Name,Price");
foreach(var item in cryptoCurrencyData) {
csvBuilder.AppendLine(string.Format("{0},"{1}"", item.Key, item.Value));
}
File.WriteAllText("C:\YourPath\Webscraping.csv", csvBuilder.ToString());
}
This creates a simple CSV with two columns: currency name and current price. Adjust the path to wherever you want the file saved.
Your Main method kicks everything off:
csharp
static async Task Main(string[] args) {
await GetDataFromWebPage();
}
Run the application and you'll see a CSV file appear with all the cryptocurrency data, neatly organized and ready to use.
This pattern works for countless scenarios. Need to monitor competitor pricing? Track product availability? Aggregate content from multiple sources? The core approach stays the same—fetch, parse, store.
The beauty of using ScraperAPI alongside HtmlAgilityPack is the combination of reliable data access and flexible parsing. You're not fighting with IP blocks or CAPTCHA challenges while simultaneously wrestling with HTML structure.
Web scraping doesn't have to be complicated. With the right tools and a clear understanding of the process, you can automate data collection tasks that would otherwise consume hours of manual work.
Start small, test thoroughly, and scale up as you understand what works for your specific use case. The 5,000 free requests give you plenty of room to experiment and refine your approach before committing to anything.