markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

Add CSS selector flag to HTML converter

Open AnupamKumar-1 opened this issue 1 year ago • 2 comments

  • This PR introduces the -s/--selector flag to the MarkItDown CLI, allowing users to specify a CSS selector to narrow the scope of HTML parsing.
  • CLI Update: Added -s/--selector flag to the CLI to allow passing a CSS selector.
  • HtmlConverter Modification: The CSS selector is passed through to the HtmlConverter for processing.
  • Scoped BeautifulSoup Parsing: When the -s/--selector flag is used, BeautifulSoup parsing is scoped to the elements matching the selector, improving parsing performance and relevance.
  • Error Handling: If no matches are found for the provided selector, a ValueError is raised to inform the user about the lack of matching elements.

How to Test:

  • Run the CLI with the -s/--selector flag
  • python -m markitdown -s 'article.entry' path/to/input.html

All related tests pass:

  • pytest -s tests/test_html_selector.py

AnupamKumar-1 avatar Apr 30 '25 22:04 AnupamKumar-1

@microsoft-github-policy-service agree

AnupamKumar-1 avatar Apr 30 '25 22:04 AnupamKumar-1