Scraping a table whose rows keep moving
· 5 min de lectura
Esta guía todavía no está disponible en tu idioma, así que estás leyendo el original en inglés.
This is the failure that catches almost everyone who scrapes a table. You grab the price in row 3, everything works, and a week later your API is quietly returning the wrong coin, the wrong product or the wrong team — because row 3 is no longer the row you meant.
Why position-based selectors rot
A CSS selector like tbody tr:nth-child(3) td:nth-child(4) says "the fourth cell of the third row". It says nothing about what that row contains. On a ranked table — market caps, sales charts, league standings — the ordering is the whole point, and it changes by design.
The dangerous part is that nothing breaks loudly. The selector still matches, the response still has a number in it, and the number is still plausible. It is simply the wrong row's number, and you may not notice for weeks.
Anchor on the identifier, not the index
The fix is to stop describing where the cell sits and start describing which row it belongs to. Every table worth scraping has a column that identifies the row: a name, a ticker, a SKU, a title. Find the row whose identifier matches, then read across to the column you want.
Read that way, the same selector survives reordering: if the row moves from third place to eleventh, the lookup follows it.
How WebToAPI asks for this
When you start picking on a page that contains a table, the extension does not let you click a value straight away. It asks a question first: which column identifies a row?
- Step one — click a cell in the identifying column. Usually the name column.
- Step two — click the values you want. Each one is stored as "the cell in this column, on the row whose identifier is X".
It is one extra click, and it is the difference between an API that keeps meaning what you meant and one that drifts silently.
When the visible name is not stable either
Sometimes the label itself changes: a site renames "Bitcoin" to "Bitcoin (BTC)", or translates it. Where the identifying cell contains a link, we prefer the link's path over its text, because a URL like /currencies/bitcoin/ tends to outlive the label rendered on top of it.
That happens automatically. If there is a usable link in the identifier cell it becomes the anchor; otherwise we fall back to matching the text.
A checklist for table APIs
- Pick an identifier column that is genuinely unique — two rows called "Other" will make the lookup ambiguous.
- Prefer a column with links in it over one with plain text.
- Avoid anchoring on a rank or index column; that is the thing that moves.
- After saving, reorder or filter the table in your browser and call the endpoint again. If the value still matches, the anchor is doing its job.
WebToAPI convierte cualquier página en un endpoint JSON: elige los valores y obtén la URL.
Añadir a Chrome
WebToAPI