Skip to main content

Library-wrapper for extracting text from Web Pages

Project description

py-web-text-extractor

CI PyPI version Python 3.14+ License: MIT

A CLI tool and Python library to extract clean text content from web pages.

Overview

py-web-text-extractor provides a simple interface for extracting the main text content from HTML documents. It can be used as a command-line tool for quick extractions or as a Python library for integration into other applications. The tool uses a fallback strategy, trying markitdown first and then trafilatura to ensure high reliability.

Features

  • Dual Extractor Strategy: Uses markitdown as the primary extractor and falls back to trafilatura for robustness.
  • CLI and Library Interface: Can be used as a standalone command-line tool or as a Python library.
  • Error Handling Modes: Supports a strict mode that raises exceptions on failure and a safe mode that returns an empty string.
  • Modern Python: Fully typed with Python 3.14+ support.

Prerequisites

  • Python 3.14 or higher.

Installation

You can install the package using pip or uv.

Using pip

pip install py-web-text-extractor

Using uv

uv add py-web-text-extractor

Usage

The tool can be used via its command-line interface or as a Python library.

Command-Line Interface (CLI)

The CLI is the quickest way to extract text from a URL.

Basic Extraction:

py-web-text-extractor https://example.com

Safe Mode:

In safe mode, the tool will return an empty string and exit gracefully if an error occurs.

py-web-text-extractor https://example.com --safe

Verbose Mode:

For troubleshooting, verbose mode provides detailed debug output.

py-web-text-extractor https://example.com --verbose

CLI Exit Codes:

Code Meaning
0 Success
1 No text content found
2 Invalid URL
3 Text extraction failed
4 Unexpected error

Python Library

For programmatic use, you can import the ExtractorService.

Quick Start:

from py_web_text_extractor.service.extractor_service import ExtractorService
from py_web_text_extractor.exception.exceptions import TextExtractionError, UrlIsNotValidException

# Initialize the service
service = ExtractorService()

# Strict mode: raises an exception on failure
try:
    text = service.extract_text_from_page("https://example.com")
    print(text)
except UrlIsNotValidException:
    print("The provided URL is not valid.")
except TextExtractionError as e:
    print(f"Failed to extract text: {e}")

# Safe mode: returns an empty string on failure
text_safe = service.extract_text_from_page_safe("https://invalid-url")
if not text_safe:
    print("Extraction failed or no content found.")

API Reference

ExtractorService

The main class for handling text extraction.

Methods:

  • extract_text_from_page(url: str) -> str: Extracts text from the given URL. Raises a TextExtractionError or UrlIsNotValidException on failure.
  • extract_text_from_page_safe(url: str) -> str: Extracts text from the given URL. Returns an empty string on failure.

Exceptions

The library uses a set of custom exceptions to allow for specific error handling.

  • TextExtractionError: Base exception for the library.
  • UrlIsNotValidException: Raised for invalid URL formats.
  • TextExtractionFailure: Raised when all extraction attempts fail.
  • MarkItDownExtractionException: Specific failure from the markitdown extractor.
  • TrafilaturaExtractionException: Specific failure from the trafilatura extractor.

Architecture

The service employs a fallback strategy to maximize reliability:

  1. It first attempts to extract content using markitdown.
  2. If markitdown fails (e.g., returns a blank string or raises an error), the service automatically retries the extraction using trafilatura.
  3. The first successful result is returned. If both extractors fail, an error is raised or an empty string is returned, depending on the mode.

Testing

To run the test suite, first install the development dependencies and then run pytest.

Patch pydub for Python 3.14 compatibility For Python 3.14 can be required patching of old dependency

python scripts/patch_pydub.py
# Install dev dependencies
uv pip install -e ".[dev]"

# Run tests
uv run pytest

# Run linters and type checkers
uv run ruff check .
uv run ruff format .
uv run mypy src/

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_web_text_extractor-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_web_text_extractor-0.1.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file py_web_text_extractor-0.1.0.tar.gz.

File metadata

  • Download URL: py_web_text_extractor-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_web_text_extractor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 26bb31457cf284da831d6fc279076b80e3ee9cbf28c949aaa2bce1c0cbcf68db
MD5 77089f661eb841a2b0278d3c63cf7ee6
BLAKE2b-256 50c5e1ceb8f4e2377d8cd8b8b506c320441a9189fb0e7459acd2d7834f3643e6

See more details on using hashes here.

File details

Details for the file py_web_text_extractor-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_web_text_extractor-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_web_text_extractor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5638c94c98cdfaa5e3e5d1395ee842f54426b0b18889eef9438010afbc994bc1
MD5 5d337c15f457fb35b6a8698530f7fd3a
BLAKE2b-256 dd198beb710e7c4f5d09cd2a31f0e8c04ca359b54eab4533c0e0dcf7000a761c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page