Web scraping sounds intimidating at first, but once you get the hang of it, extracting data from websites becomes surprisingly straightforward. Today we're going to walk through building a real-world scraper that pulls cryptocurrency prices from CoinMarketCap and saves them to a CSV file.
Before we dive into code, let's talk about why you'd want to scrape data in the first place. Maybe you're tracking product prices across different sites, monitoring competitor information, or collecting research data. Whatever the reason, doing this manually is tedious and time-consuming. That's where automated scraping comes in.
The challenge? Modern websites don't always play nice with scrapers. They might block your IP, throttle your requests, or serve you different content than what appears in a browser. This is exactly where a service like 👉 ScraperAPI makes web scraping effortless by handling proxy rotation and request management automatically.
Think of ScraperAPI as your scraping assistant that handles all the annoying technical details. Instead of managing proxy servers and dealing with blocked requests yourself, you send your target URL to their API and get back clean HTML. Simple as that.
Here's what happens behind the scenes:
IP rotation with every request - No more worrying about getting blocked
Automatic retries - Failed requests get handled without you lifting a finger
Geolocation support - Scrape as if you're browsing from different countries
Unlimited bandwidth - Scrape as much data as you need
Custom headers and sessions - Full control when you need it
For developers working in C#, this means you can focus on parsing and using the data rather than fighting infrastructure problems.
First things first, you'll need an account. Head over to ScraperAPI's signup page and create one. The free tier gives you 1000 requests per month, which is plenty for testing and small projects.
Once you're in, you'll see your dashboard with your API key front and center. Keep this handy - you'll need it in a moment.
We're going to create a console application that scrapes cryptocurrency data from CoinMarketCap. The goal is to extract coin names and their current prices, then save everything to a CSV file.
Create a new Console App in Visual Studio. You can use .NET Core or .NET Framework - either works fine.
Next, install two essential NuGet packages. Open the Package Manager Console and run:
Install-Package ScraperApi
Install-Package HtmlAgilityPack
The first package is ScraperAPI's official C# SDK. The second, HtmlAgilityPack, is a fantastic library for parsing HTML documents in .NET.
Here's where the magic happens. We'll create a method that uses 👉 ScraperAPI's HTTP client to fetch web pages without getting blocked:
csharp
static async Task GetDataFromWebPage() {
try {
Console.WriteLine("### Started Getting Data.");
string apiKey = "**Add 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);
}
} catch (Exception ex) {
Console.WriteLine("GetDataFromWebPage Failed: {0}", ex.Message);
}
}
Replace **Add Your API Key Here** with your actual API key from the dashboard. The GetProxyHttpClient() method creates an HTTP client that routes all requests through ScraperAPI's infrastructure. That means proxy rotation, retry logic, and all the other features work automatically.
Once we have the HTML, we need to extract the data we care about. This is where HtmlAgilityPack shines:
csharp
static void ParseHtml(string htmlData) {
try {
Console.WriteLine("### Started Parsing HTML.");
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);
} catch (Exception ex) {
Console.WriteLine("ParseHtml Failed: {0}", ex.Message);
}
}
This code loads the HTML into a document object, finds the table body containing cryptocurrency data, then loops through each row extracting the name and price. The XPath queries (SelectSingleNode and SelectNodes) make it easy to navigate the HTML structure.
The final step is writing our extracted data to a CSV file:
csharp
static void WriteDataToCSV(Dictionary<string, string> cryptoCurrencyData) {
try {
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:\\Cryptocurrency-Prices.csv", csvBuilder.ToString());
Console.WriteLine("### Completed Writting Data To CSV File.");
} catch (Exception ex) {
Console.WriteLine("WriteDataToCSV Failed: {0}", ex.Message);
}
}
This creates a CSV with two columns: Name and Price. You can easily modify this to capture additional data points like market cap or 24-hour price changes.
Your Main method should look like this:
csharp
static async Task Main(string[] args) {
await GetDataFromWebPage();
}
Run the application and you'll see a CSV file created at the specified path with all the cryptocurrency prices neatly organized.
Open the generated CSV file and you'll find a clean two-column spreadsheet with cryptocurrency names and their current prices. From here, you could import this into Excel for analysis, feed it into a database, or use it as input for a trading algorithm.
The beauty of this approach is how maintainable it is. Need to scrape a different website? Just change the URL and update your parsing logic. Want to capture more data fields? Modify the XPath queries. The core infrastructure remains stable and reliable.
Web scraping in C# doesn't have to be complicated. With ScraperAPI handling the infrastructure challenges and HtmlAgilityPack simplifying HTML parsing, you can focus on extracting and using data rather than fighting technical obstacles.
This same pattern works for countless scraping scenarios - price monitoring, content aggregation, research data collection, and more. The key is starting simple, testing thoroughly, and expanding gradually as you get comfortable with the tools.