Quick Start: C# / .NET
Make your first API call with C# and parse the HTML response with AngleSharp.
Prerequisites
- .NET 6.0+ (for top-level statements and System.Net.Http.Json)
- A FastWebScraper API key (get one free)
Installation
Install AngleSharp for HTML parsing. HttpClient and System.Net.Http.Json are built into .NET 6+.
dotnet add package AngleSharpSync Scraping (Simple)
The sync endpoint waits for the scrape to complete and returns the HTML in a single response. Best for quick scripts and testing.
// Program.cs
using System.Net.Http.Json;
using System.Text.Json;
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(60) };
client.DefaultRequestHeaders.Add("X-API-Key", "YOUR_API_KEY");
var request = new
{
url = "https://example.com",
mode = "auto"
};
var response = await client.PostAsJsonAsync("https://api.fastwebscraper.com", request);
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Error: {error}");
return;
}
var result = await response.Content.ReadFromJsonAsync<JsonElement>();
var data = result.GetProperty("data");
Console.WriteLine($"Status code: {data.GetProperty("statusCode")}");
Console.WriteLine($"HTML length: {data.GetProperty("html").GetString()?.Length}");Async Scraping (Production)
The async endpoint returns a job ID immediately. Poll for the result when it's ready. Use this for production workloads and batch processing.
// Program.cs
using System.Net.Http.Json;
using System.Text.Json;
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "YOUR_API_KEY");
// Step 1: Submit the scrape job
var request = new
{
url = "https://example.com",
mode = "auto",
waitForSelector = ".main-content"
};
var submitResponse = await client.PostAsJsonAsync("https://api.fastwebscraper.com", request);
var submitResult = await submitResponse.Content.ReadFromJsonAsync<JsonElement>();
var jobId = submitResult.GetProperty("data").GetProperty("jobId").GetString();
Console.WriteLine($"Job submitted: {jobId}");
// Step 2: Poll for the result
JsonElement job;
string status;
do
{
await Task.Delay(2000);
job = await client.GetFromJsonAsync<JsonElement>(
$"https://api.fastwebscraper.com/{jobId}");
status = job.GetProperty("data").GetProperty("status").GetString()!;
Console.WriteLine($"Status: {status}");
} while (status is "PENDING" or "IN_PROGRESS");
if (status == "COMPLETED")
{
var html = job.GetProperty("data").GetProperty("html").GetString()!;
Console.WriteLine($"HTML length: {html.Length}");
}
else
{
Console.WriteLine("Job failed");
}Parsing HTML with AngleSharp
Once you have the HTML, use AngleSharp to extract structured data. AngleSharp provides a standards-compliant HTML parser with a DOM API similar to browser JavaScript.
using AngleSharp;
using AngleSharp.Html.Parser;
// Assuming 'html' contains the scraped HTML string
var parser = new HtmlParser();
var document = await parser.ParseDocumentAsync(html);
// Extract text content
var pageTitle = document.QuerySelector("h1")?.TextContent.Trim();
Console.WriteLine($"Page title: {pageTitle}");
// Extract all links
var links = document.QuerySelectorAll("a[href]")
.Select(a => new
{
Text = a.TextContent.Trim(),
Href = a.GetAttribute("href")
})
.ToList();
Console.WriteLine($"Links found: {links.Count}");
// Extract product data (example)
var products = document.QuerySelectorAll(".product-card")
.Select(card => new
{
Name = card.QuerySelector(".product-name")?.TextContent.Trim(),
Price = card.QuerySelector(".product-price")?.TextContent.Trim(),
Url = card.QuerySelector("a")?.GetAttribute("href"),
})
.ToList();
Console.WriteLine($"Products: {products.Count}");
// Extract table data
var tableRows = document.QuerySelectorAll("table tbody tr")
.Select(row => row.QuerySelectorAll("td")
.Select(cell => cell.TextContent.Trim())
.ToArray())
.Where(cells => cells.Length > 0)
.ToList();
Console.WriteLine($"Table rows: {tableRows.Count}");Complete Example: Scrape and Parse
Here's a complete program that scrapes a page and extracts structured data.
// Program.cs
using System.Net.Http.Json;
using System.Text.Json;
using AngleSharp.Html.Parser;
var apiKey = "YOUR_API_KEY";
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(60) };
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
var scraped = await ScrapeAndParse(client, "https://store.example.com/products");
Console.WriteLine(JsonSerializer.Serialize(scraped, new JsonSerializerOptions
{
WriteIndented = true
}));
async Task<object> ScrapeAndParse(HttpClient http, string url)
{
// Scrape the page
var request = new
{
url,
mode = "auto",
waitForSelector = ".product-card"
};
var response = await http.PostAsJsonAsync("https://api.fastwebscraper.com", request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<JsonElement>();
var html = result.GetProperty("data").GetProperty("html").GetString()!;
// Parse the HTML
var parser = new HtmlParser();
var document = await parser.ParseDocumentAsync(html);
var products = document.QuerySelectorAll(".product-card")
.Select(card => new
{
Name = card.QuerySelector(".product-name")?.TextContent.Trim(),
Price = card.QuerySelector(".price")?.TextContent.Trim(),
Image = card.QuerySelector("img")?.GetAttribute("src"),
Link = card.QuerySelector("a")?.GetAttribute("href"),
})
.ToList();
return new
{
Url = url,
ScrapedAt = DateTime.UtcNow.ToString("o"),
Products = products
};
}Error Handling
Always handle errors and implement retries for production use.
using System.Net.Http.Json;
using System.Text.Json;
async Task<JsonElement> ScrapeWithRetry(
HttpClient client, string url, int retries = 3)
{
for (int attempt = 1; attempt <= retries; attempt++)
{
try
{
var request = new { url, mode = "auto" };
var response = await client.PostAsJsonAsync(
"https://api.fastwebscraper.com", request);
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
{
// Rate limited — wait and retry
var delay = (int)Math.Pow(2, attempt) * 1000;
Console.WriteLine($"Rate limited. Waiting {delay}ms...");
await Task.Delay(delay);
continue;
}
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<JsonElement>();
}
catch (Exception ex)
{
if (attempt == retries) throw;
var delay = (int)Math.Pow(2, attempt) * 1000;
Console.WriteLine($"Attempt {attempt} failed: {ex.Message}. Retrying in {delay}ms...");
await Task.Delay(delay);
}
}
throw new Exception($"Failed after {retries} retries");
}Concurrent Scraping (Advanced)
For scraping multiple URLs concurrently, use Task.WhenAll with a semaphore to limit concurrency.
using System.Net.Http.Json;
using System.Text.Json;
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "YOUR_API_KEY");
var urls = new[]
{
"https://example.com/page/1",
"https://example.com/page/2",
"https://example.com/page/3",
"https://example.com/page/4",
"https://example.com/page/5",
};
// Limit to 3 concurrent requests
var semaphore = new SemaphoreSlim(3);
var tasks = urls.Select(async url =>
{
await semaphore.WaitAsync();
try
{
var request = new { url, mode = "auto" };
var response = await client.PostAsJsonAsync("https://api.fastwebscraper.com", request);
var result = await response.Content.ReadFromJsonAsync<JsonElement>();
var jobId = result.GetProperty("data").GetProperty("jobId").GetString();
Console.WriteLine($"Queued {url} -> Job {jobId}");
return new { Url = url, JobId = jobId };
}
finally
{
semaphore.Release();
}
});
var results = await Task.WhenAll(tasks);
Console.WriteLine($"Submitted {results.Length} jobs");Next Steps
- API Reference — all endpoints, parameters, and response schemas
- Async vs Sync Guide — when to use each scraping mode
- Error Handling Guide — production-ready retry patterns
- Use Cases — industry-specific scraping examples