Remove HTML Elements From a Scrape Response
When extraction rules keep tripping over banners, scripts, or style blocks, deleting those nodes in the browser can leave a leaner document to parse.
This guide shows how to target one element type or several at once, and where the JavaScript Scenario fits in the request.
Why clean the HTML during the scrape
Unwanted markup can make extraction rules harder to write and harder to trust. A banner, an embedded script, or a block of styles may sit between the content you need and the selectors you planned to use. Removing those nodes while the page is still being scraped means the returned HTML already reflects the cleanup, rather than requiring a separate post-processing step.
How the evaluate instruction works
The cleanup is expressed as a JavaScript Scenario. Inside that scenario, an evaluate instruction runs a short browser snippet against the live page. The snippet uses querySelectorAll() to collect every element matching a CSS selector, then iterates over that collection and calls remove() on each element. Because this runs before the final HTML is captured and returned, the response you receive no longer contains the deleted nodes.
Removing a single kind of element
Start with the general pattern supplied as protected module code0001, replacing the placeholder selector with your own CSS selector. For a concrete case, protected module code0002 shows the same structure aimed at style elements, which is useful when embedded styling is cluttering the document you want to parse. In both cases the shape stays the same: one instructions array, one evaluate entry, one selector-driven removal.
Removing multiple kinds of elements at once
You are not limited to a single selector. Combining selectors with commas inside one querySelectorAll call lets one pass handle several element types. Protected module code0003 shows the combined browser snippet covering style, script, and a class-based banner, and protected module code0004 shows that same combined selector placed into the evaluate instruction of the JavaScript Scenario. The result is that all matched kinds are removed together in one request.
Checklist before you scrape
Before you run a request, work through this checklist: identify the exact tag or class you want gone; confirm the selector matches only disposable markup and not content your extraction rules depend on; decide whether one selector is enough or a comma-combined list is clearer; place the snippet in the evaluate instruction of the JavaScript Scenario; and inspect the returned HTML to confirm the targeted elements are absent while the needed content remains intact.
Frequently asked questions
Can one request remove several different kinds of elements?
Place the combined selector inside the single evaluate instruction of the JavaScript Scenario. The browser then matches every element covered by any part of that list and removes each match before the final HTML is captured.
How does removal happen before the HTML is returned?
Use a CSS selector that matches the elements you want gone, such as a tag name or a class. The JavaScript Scenario runs during the scrape, so removal happens in the page before the HTML response is returned.
{"instructions":[{"evaluate":"document.querySelectorAll('ELEMENT-CSS-SELECTOR').forEach(function(e){e.remove();});"}]}
{"instructions":[{"evaluate":"document.querySelectorAll('style').forEach(function(e){e.remove();});"}]}
{"instructions":[{"evaluate":"document.querySelectorAll('style, script, .ad-banner').forEach(function(e){e.remove();});"}]}