Sometimes you may need to remove specific HTML elements from the page's content to get cleaner results for your data extraction rules, or to simply delete unnecessary content from your response.
ScrapingBee lets you remove website elements during the scrape itself, before it even returns the HTML. To implement that, use a JavaScript Scenario with an evaluate instruction that runs the snippet below.
{"instructions":[{"evaluate":"document.querySelectorAll('ELEMENT-CSS-SELECTOR').forEach(function(e){e.remove();});"}]}
querySelectorAll() finds every element matching that selector, and .forEach(...e.remove()) deletes each one from the page before ScrapingBee captures and returns the final HTML.
For example, to remove all of the <style> elements from the response, the value of your js_scenario parameter would look like this:
{"instructions":[{"evaluate":"document.querySelectorAll('style').forEach(function(e){e.remove();});"}]}
Removing multiple HTML elements
You're not limited to one selector. To remove several kinds of elements in a single request, combine the selectors with commas inside one querySelectorAll call:
document.querySelectorAll('style, script, .ad-banner').forEach(function(e){e.remove();});
Drop the combined selector into the same evaluate instruction shown earlier, and ScrapingBee removes all three kinds of elements in one pass:
{"instructions":[{"evaluate":"document.querySelectorAll('style, script, .ad-banner').forEach(function(e){e.remove();});"}]}