Extract HTML Table Content in Python
HTML tables are one of the most common ways data appears on the web.
Their rows and columns make them readable for people, but the same structure is what makes them useful for a Python workflow. This guide shows how to target a table with a CSS selector inside extract_rules and receive the table content as structured output instead of parsing the raw HTML response yourself.
What you are extracting
A table on a web page is made of rows and cells. When you extract it, you are asking for the table's content in a form your Python code can use. The example in this guide targets a stock-price table from a demo page and shows two output shapes: a JSON representation and an array representation. Both come from the same table, so the difference is how the rows are organized, not which table is read.
Inputs: selector and output mode
The request uses extract_rules as a parameter. Inside it, each rule has a selector and an output. The selector is the CSS selector of the table you want. The output tells the service which representation to return. In the example, the selector is .table for both rules, and the two output values are table_json and table_array. The demo page is also referenced with the .BasicTable-table selector, so check which selector actually matches the table on the page you are working with.
Decision checklist: JSON or array?
Use this checklist before you pick an output mode:
- Choose table_json when you want each row to keep named keys such as SYMBOL, NAME, PRICE, CHANGE and %CHANGE.
- Choose table_array when you want each row as a plain list of values in column order.
- Use both in the same request when you need named access for some steps and positional access for others.
- Check the column order in the source table before relying on array positions.
- Confirm that the selector matches the actual table, not a wrapper element.
Example output
The response contains both keys. Under table_json, each row is an object with named fields. Under table_array, each row is a list of values. The sample shows AMD with the name Advanced Micro Devices Inc, a price of 94.82, a change of -3.98 and a percent change of -4.03. The ellipsis indicates that more rows follow. The exact field names and values are part of the captured example, so treat them as an illustration of the shape rather than a live quote.
What happens after the request
When the response is successful, the table content is already structured. You can print it, pass it to further data manipulation, or store it. The key point is that you do not need to write an HTML parser for the table cells. If you need more detail on how the two representations differ, the data extraction documentation covers JSON representation and array representation separately.
| Output | Row shape | Best for |
|---|---|---|
| table_json | Object with named keys | Reading fields by name |
| table_array | List of values in column order | Positional processing |
The example uses a demo table; field names and values are illustrative.
Frequently asked questions
Do I need to parse the HTML response myself?
No. The table content is returned in a structured representation, so you can work with the rows and cells directly.
Can I request both JSON and array output at once?
Yes. The example defines two rules inside extract_rules, one for table_json and one for table_array, and both appear in the response.
What selector should I use?
Use the CSS selector that matches the table you want. The example uses .table, while the demo page is described with .BasicTable-table, so verify the selector against the page.