keepcalculator

06 Numbers and Powers

How to Make an AI Agent Capture Only Main Content

Card No. 06·60580 · Updated · Standard formula, shown below

To make an AI agent capture only a webpage’s main content, extract the article or primary information while excluding navigation, ads, sidebars, and footers. You’ll need a browser or page-fetching tool and a way to inspect the page’s HTML; the most reliable approach combines semantic HTML, readability extraction, and a fallback for unusual pages.

What You’ll Need

  • An agent that can fetch webpages or control a browser.
  • A programming environment for processing HTML, if you are building the extraction yourself.
  • A readability or content-extraction library, or an equivalent extraction feature in your framework.
  • Several representative pages to test, including an article, a page with lots of navigation, and a page that needs JavaScript.
  • Permission to access the pages, and a plan to respect site terms, robots rules, and rate limits.

Step-by-Step

  1. Decide what “main content” means for your task. Write down what the agent should keep: for example, an article’s title, author, publication date, and body text. Decide whether captions, tables, code examples, product details, or comments count. Also list what to remove, such as menus, cookie notices, advertisements, related links, and repeated headers. Clear rules make extraction easier to evaluate.
  2. Fetch the page in the right way. For a static page, request its HTML and parse the response. If the content appears only after scripts run, use a browser automation tool to load the page and wait for the main content to appear before reading the rendered DOM. Avoid waiting a fixed, unnecessarily long time on every page; prefer a relevant readiness condition, such as the article element becoming available. Handle failed requests, redirects, and access-denied pages as errors rather than treating their text as an article.
  3. Parse HTML, not raw text. Load the response into an HTML parser and inspect its document structure. Do not extract by deleting everything between text strings or by relying on regular expressions: markup can be nested, attributes can change, and visible text may be mixed with scripts and styles. Remove non-content elements such as script, style, noscript, and, where appropriate, nav, footer, and aside. Be cautious with aside: some sites use it for material that is important to the page.
  4. Look for semantic content first. Check for an <article> element, or a <main> element containing the page’s primary content. Within the likely region, identify the main heading and paragraphs, lists, tables, and figures that belong to the requested material. Semantic tags are useful signals, not guarantees: a page may omit them, or use them for only part of its content.
  5. Use a readability extractor as the general fallback. Pass the cleaned or original document to a maintained readability library. These tools estimate which region is the article using features such as text density, paragraph length, and link frequency. Keep the returned title and content separately where available. Treat the result as a candidate, not unquestionable truth; some pages are too short, unusual, or interactive for reliable automatic detection.
  6. Apply site-specific selectors only when necessary. If a particular site consistently defeats general extraction, inspect a few pages and identify a stable container selector, such as a site’s article-body class. Use a small, documented set of domain-specific rules before falling back to readability. Avoid selectors based on fragile details such as an autogenerated class name or a specific element position. Recheck these rules when the site changes.
  7. Clean the extracted region without stripping meaning. Remove elements that are clearly decorative or unrelated, then convert the remaining content into a consistent representation, such as plain text or structured blocks. Preserve headings, paragraph boundaries, list items, table relationships, and useful image captions. Do not simply concatenate every text node: that can merge sentences, destroy table context, and make the result hard for the agent to interpret.
  8. Check that the extraction is plausible. Compare the extracted text with the page title and the visible main area. Useful checks include whether the result is empty, unusually short, dominated by links, or mostly boilerplate. Set sensible size limits, but do not use a single minimum length that rejects valid short pages. If extraction looks poor, try the rendered DOM, a site-specific selector, or a clear failure response instead of silently passing navigation text to the agent.
  9. Pass the result to the agent with its source. Provide the clean content along with the page URL and, when available, title and retrieval time. Keep the original page separate from the extracted text so the agent can be given a controlled fallback. Instruct the agent to answer from the captured content and to say when the page does not contain the requested information; extraction cannot recover content that was never fetched.
  10. Test and monitor across page types. Maintain a small test set with expected titles and key passages. Check both false inclusions (menus or recommendations) and false exclusions (captions, tables, or the final paragraphs). Log extraction failures and the method used, while avoiding unnecessary storage of personal or sensitive page data. Re-run the tests after changing the browser, parser, library, or site rules.

Common Mistakes

  • Taking all visible text: a page’s full text often includes menus, cookie banners, and related stories. Identify a content region before extracting.
  • Assuming one HTML tag is always right: <main> and <article> can be missing, nested, or used inconsistently. Combine semantic signals with readability and validation.
  • Removing too much: broad rules that delete every div, heading, or sidebar can erase the article itself. Remove known noise elements, not whole classes of useful structure.
  • Ignoring JavaScript-rendered content: the initial HTML may contain only a shell. Check the rendered page when the article is absent from the fetched response.
  • Trusting extraction without checks: a library can return a recommendation panel or a short excerpt. Validate the title, length, and content shape.
  • Using brittle selectors: selectors tied to changing class names break silently. Prefer semantic structure and keep site-specific rules easy to review.

Tips

  • Keep extraction and the agent’s reasoning as separate stages. This makes it easier to identify whether a poor answer came from capture or interpretation.
  • Preserve a structured form when the agent needs to cite or compare details: headings, paragraphs, lists, and tables are more useful than one long text string.
  • Use a clear fallback status such as “no main content detected” rather than returning a blank result or the entire page.
  • Record the extraction method and basic quality signals for debugging, but avoid logging full page contents unless needed and appropriate.
  • Test pages from different layouts and languages. A rule that works for one publisher may not work for another.

Troubleshooting

The result is empty. Check whether the request succeeded and whether the page requires JavaScript, authentication, or a consent action. Inspect the rendered DOM and confirm that your wait condition targets an element that actually appears. If access is blocked, do not attempt to bypass it; report that the content could not be retrieved.

The result contains menus or ads. Narrow the candidate region to the article or main container, remove known navigation elements, and run readability extraction. Check whether a cookie banner or recommendation area is being mistaken for the body.

Important sections are missing. Confirm that cleanup rules are not deleting headings, tables, figures, or elements that the site uses for article text. Compare the extracted region with the rendered page and adjust the rule narrowly.

It works on one site but fails on another. Keep a general readability fallback and add a specific selector only for a verified layout. Track the domain and failure type so you can distinguish a page change from a fetching problem.

The agent still gives unsupported answers. Inspect the captured content first. If the needed detail is absent, have the agent state that limitation rather than infer from unrelated page text.

Frequently Asked Questions

Can an AI agent identify main content without a special library?
Yes, but it must parse the HTML and apply its own rules. A readability library is often a practical starting point because it handles many layouts better than a few hand-written selectors.

Should I use the page’s <main> element?
Use it as a strong clue, then check that it contains the expected article or primary information. Site markup is not always consistent.

Do I need a headless browser?
Only when the needed content is missing from the fetched HTML or depends on JavaScript rendering. Static pages can usually be processed without one.

How can I tell whether extraction worked?
Check for a plausible title, useful body text, and expected sections, while watching for boilerplate or excessive links. Test against known pages rather than relying on one length threshold.

Can the method capture information behind a login?
Only if the agent has legitimate access and the page can be retrieved through an authorized session. Do not try to evade access controls.

Will this work on every webpage?
No. Interactive pages, unusual layouts, and blocked or inaccessible content may require a different approved retrieval method or a clear failure response.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →