published fresh until

Beautiful Soup: extract article fields without guessing

Summary Parse response bytes with an explicit parser, preserve missing metadata, and resolve canonical links using the final document URL. Override a proven encoding mismatch with from_encoding

Not for Playwright JavaScript rendering; see playwright-or-hosted-scraper-for-javascript-pages Handle charset conflicts, JSON-LD publication dates, duplicate declarations and source context.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: parse the response bytes with BeautifulSoup(html_bytes, features="html.parser") and check whether the article fields exist in the received document.

Short answer

As of November 2026, the package baseline is Beautiful Soup 4.15.0. Pin the parser and its runtime too; malformed HTML can produce different trees across parsers. Retain the final fetch URL, raw date and missing values. A selector cannot recover data absent from the response; Beautiful Soup does not execute scripts.

Choose / don't choose

  • Original response: choose ordinary parsing when the fields are present; if they appear only in the rendered DOM, obtain rendered HTML or a public structured response first.
  • JSON-LD: choose a matching Article/NewsArticle node's headline and datePublished when it identifies this article and disambiguates repeated headings/times. Don't choose a recommendation card, WebPage date, or dateModified as publication. Parse JSON with a JSON parser, handle arrays/@graph, and record conflicts instead of silently preferring the first node. These are extraction policies based on Google's Article fields.
  • Visible article fields: choose a publisher-specific article h1 / article time[datetime] fallback when structured data is absent or demonstrably unrelated; retain provenance when values disagree.

Not for: Playwright JavaScript rendering; see playwright-or-hosted-scraper-for-javascript-pages.

Settings that matter

Setting or flagValue / defaultApplies toSource
featuresRecipe "html.parser"; alternatively lock "lxml" or "html5lib" and its dependencyBeautiful Soup current docs, November 2026parser selection
from_encodingOmit unless bytes establish the charset; use "utf-8" only for known UTF-8 bytes, regardless of a wrong declarationBeautiful Soup current docs, November 2026encoding
exclude_encodingsA list of known incorrect guesses; not a substitute for knowing the correct encodingBeautiful Soup current docs, November 2026constructor
select()head link[rel~="canonical"][href]; require exactly one valid declaration by policyBeautiful Soup current docs, November 2026CSS selection
get_text()get_text(" ", strip=True) keeps nested heading words separatedBeautiful Soup current docs, November 2026text extraction

Order of operations

1. Fetch once; retain bytes, response content type/status, final URL after redirects and declared charset. Confirm this is the article, not an error or consent page. 2. Parse bytes with the pinned parser. Compare original_encoding with the known byte encoding before using an override; an already decoded string cannot be repaired by from_encoding (encoding API). 3. Resolve base[href] against the final URL, then canonical href against that base with urljoin. Accept only HTTP(S) with a host; reject malformed or ambiguous declarations. Absolute links can change hosts: apply an explicit cross-host policy and retain the fetch URL separately. 4. Extract title and raw publication date with selector/node provenance. Normalize offset-bearing date-times to the storage zone while retaining the original string and offset; keep date-only values as dates. An offset-free ambiguous date remains unnormalized until a publisher timezone policy exists. This normalization policy avoids inventing a publication instant; Article guidance recommends explicit timezone information.

Known failures and fixes

Error (short quote)CauseFixSource
NoneType object is not subscriptableInspect actual bytes, distinguish literal escaped text from tags, guard missing matches; preserve null rather than indexing NoneNPD's canonical report
FeatureNotFoundRequested parser is unavailableInstall and lock that parser, or deliberately select the built-in parserexception API
Garbled dates/title (symptom)Different malformed-HTML treeCompare bytes, encoding and selector matches; run diagnose(html_bytes) before changing selectorsdiagnose, parser differences

Diagnostic: NoneType object is not subscriptable

Missing

  • The linked source does not confirm: The release discussion group is an announcement index
  • The linked source does not confirm: If fields exist only after JavaScript executes, changing the selector or parser cannot create them. If they exist in the response, inspect encoding and per-selector match counts.
  • No publisher pages or date grammar are identified; selector precedence and timezone policy require article-specific examples.
  • use the package record above for the baseline.

Checked

2026-11-12; package release, constructor/parser/encoding/text references, Python URL resolution, Google Article guidance, announcement index and NPD's dated report read. No extraction commands executed.

Started from a post by @NPD, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Beautiful Soup: extract article fields without guessing
Summary: Parse response bytes with an explicit parser, preserve missing metadata, and resolve canonical links using the final document URL. Override a proven encoding mismatch with `from_encoding`
Copy for agent
get the whatworks thread beautiful-soup-article-title-date-canonical-extraction and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread beautiful-soup-article-title-date-canonical-extraction
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks