Extract Website Table Data with a .NET API
Aspose.HTML for .NET
can load a web page directly into an
HTMLDocument
. A C# application can then locate tables with CSS selectors, read their rows and cells from the loaded DOM, and save the collected values as text, CSV, JSON, or application-specific records.
Extract a Table from a Web Page in C#
The following example loads a public web page, selects its first table, and writes each row to website-table.txt. Replace the generic table selector with an ID, class, or structural selector when the page contains several tables.
C# Code to Extract Website Table Data
using Aspose.Html;
using Aspose.Html.Dom;
using System;
using System.IO;
string pageUrl = "https://docs.aspose.com/html/net/how-to-extract-html-tables/";
// Load the web page from its URL
using HTMLDocument document = new HTMLDocument(pageUrl);
// Select the table to extract
Element table = document.QuerySelector("table");
if (table == null)
{
throw new InvalidOperationException("The loaded page does not contain a table.");
}
using StreamWriter writer = new StreamWriter("website-table.txt");
// Write one line for each table row
foreach (Element row in table.QuerySelectorAll("tr"))
{
string line = string.Empty;
foreach (Element cell in row.QuerySelectorAll("th, td"))
{
line += line.Length == 0 ? cell.TextContent.Trim() : " | " + cell.TextContent.Trim();
}
if (line.Length > 0)
{
writer.WriteLine(line);
}
}
Steps to Extract a Table from a Website
- Pass the web page URL to an
HTMLDocumentconstructor. - Locate the required table with
QuerySelector()or collect all matching tables withQuerySelectorAll(). - Select the
<tr>elements inside the table, then read the<th>and<td>cells in each row. - Trim each cell’s
TextContentand join the values in their document order. - Write the extracted rows to a text file or pass the values to the output workflow required by the application.
Website Table Extraction Considerations
- Use a stable selector. Pages may contain navigation, layout, and related-content tables. Prefer an ID, class, or content-area selector such as
main tablewhen possible. - Choose one or every table.
QuerySelector()returns the first match, whileQuerySelectorAll("table")supports processing every table in document order. - Inspect the loaded DOM. The required table must exist in the document available after loading. Content inserted later by complex client-side scripts may not be present in the selected DOM.
- Plan for network conditions. Remote loading can fail because of an unavailable URL, authentication requirements, access restrictions, or other request errors.
- Choose the required output. The example writes readable text, but the same extracted cell values can be serialized to JSON, escaped for CSV, stored in a database, or passed to another component.
Website Table Extraction Documentation
See Extract HTML Tables in C# for examples that load local files and web page URLs, select tables, preserve headers, and extract links. The Data Extraction section covers CSS selectors, XPath, DOM navigation, resources, and other website extraction tasks.
Get Started with Aspose.HTML for .NET API
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
Extract Tables from Websites FAQ
1. Can Aspose.HTML for .NET extract a table directly from a web page URL?
Yes. Pass an HTTP or HTTPS URL to the HTMLDocument constructor, then query the loaded DOM for the required <table> element. The application does not need to download the HTML into a separate file first.
2. Why is a table visible in my browser but missing from the loaded HTML document?
The table may be inserted after page load by client-side code or may require authentication or another request context. Inspect the loaded DOM first and confirm that the expected <table> element is actually present.
3. How do I extract a specific table from a website?
Use a stable CSS selector such as #results, .price-table, or main table with QuerySelector(). This avoids accidentally extracting navigation or layout tables that appear earlier in the document.
4. Can I extract all tables from the same web page?
Yes. Use QuerySelectorAll("table"), iterate through the returned elements in document order, and process the rows and cells of each table separately.
5. Can website table data be saved as CSV or JSON?
Yes. The DOM API supplies the cell values without imposing an output format. Escape and write them with a CSV workflow, serialize them with System.Text.Json, or send them to the storage layer used by the application.
Related HTML Table Workflows
Create, edit, or process tables from local HTML documents with focused Aspose.HTML for .NET guides.