Skip to main content

A pure Python HTML parser and scraper (BeautifulSoup alternative)

Project description

PureScraper 🧠

A pure Python HTML scraper that replaces BeautifulSoup for simple data extraction — no external dependencies required for core functionality.

🚀 Features

  • Fetches HTML using raw sockets (no requests library needed)
  • Parses HTML into a traversable DOM tree
  • Cleans and prettifies malformed HTML
  • Removes invalid Unicode / hidden characters
  • Extracts data: links, images, tables, and JSON from scripts
  • Optional caching with configurable expiry
  • Rate limiting support
  • Fully extensible and reusable as a local or pip package

📦 Installation

Local install

pip install .

From PyPI

pip install pure-scraper

🎯 Quick Start

Basic Usage

from pure_scraper.parser import PureScraper
from pure_scraper.fetcher import fetch_html

# Fetch and parse HTML
html = fetch_html("https://example.com")
scraper = PureScraper(html)

# Find elements
links = scraper.get_links()
images = scraper.get_images()
tables = scraper.get_tables()

# Extract text
text = scraper.get_text()

# Pretty print HTML
print(scraper.prettify())

CLI Usage

pure-scraper https://example.com

📚 API Reference

PureScraper Class

Methods

  • find(tag=None, attrs=None) - Find first element matching tag and attributes
  • find_all(tag=None, attrs=None) - Find all elements matching tag and attributes
  • get_text() - Extract all text content
  • get_links(tag="a") - Extract all links with text and href
  • get_images(tag="img") - Extract all images with src and alt text
  • get_tables() - Extract table data as nested lists
  • get_html() - Get prettified HTML string
  • prettify() - Alias for get_html()

Attribute Matching

Find elements using attribute selectors:

# Exact match
scraper.find("div", attrs={"class": "container"})

# Regex match
scraper.find("div", attrs={"class": "regex:^item-.*"})

fetch_html(url, timeout=None, use_cache=None, max_attempts=3)

Fetches HTML from a URL with automatic retry and caching support.

Parameters:

  • url (str) - URL to fetch
  • timeout (int) - Socket timeout in seconds (default: 30)
  • use_cache (bool) - Enable caching (default: True)
  • max_attempts (int) - Number of retry attempts (default: 3)

Returns: HTML string

Configuration

Set via pure_scraper.config:

from pure_scraper.config import config

config.timeout = 30                    # Socket timeout
config.use_cache = True                # Enable caching
config.cache_expiry_hours = 24         # Cache expiry time
config.rate_limit_seconds = 1          # Delay between requests
config.show_logs = True                # Show fetch logs
config.user_agent = "Custom Agent"     # Custom User-Agent

🏗️ Project Structure

pure_scraper/
├── __init__.py          # Package exports
├── parser.py            # HTML parser and DOM tree
├── fetcher.py           # HTTP fetching with socket
├── cache.py             # Caching utilities
├── config.py            # Configuration
├── utils.py             # Utility functions
└── cli.py               # Command-line interface

⚙️ Technical Details

Socket-Based HTTP

  • Uses Python's built-in socket and ssl modules
  • Supports HTTP/1.1 with automatic redirects
  • Handles gzip and deflate compression
  • Optional Brotli support

DOM Tree Structure

  • Hierarchical Node objects representing HTML elements
  • Parent-child relationships preserved
  • Efficient traversal and searching

Caching

  • File-based caching with configurable expiry
  • Automatic cache invalidation based on age
  • Optional per-request cache bypass

🔧 Development

Running Tests

python test.py

Building Package

python -m build

Installation for Development

pip install -e .

📝 License

MIT License - see LICENSE file for details

🤝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

⚠️ Notes

  • This is a lightweight alternative to BeautifulSoup for simple scraping tasks
  • Not recommended for heavily JavaScript-rendered content (use Selenium/Playwright for that)
  • Respects robots.txt and be mindful of rate limiting
  • Always check the target website's terms of service before scraping

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

pure_scraper-2.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

pure_scraper-2.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pure_scraper-2.0.0.tar.gz.

File metadata

  • Download URL: pure_scraper-2.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pure_scraper-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3738ef80282dc601115f9873ff0ea5fbe009e743d0905e6f93b6de413e27f69b
MD5 644de9b812c260e22bfacdc7ab9597f3
BLAKE2b-256 2c01a7b651d9ee85d8edbf78e17865eb83d188b1effae4cc980a2923ae1fc179

See more details on using hashes here.

File details

Details for the file pure_scraper-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pure_scraper-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pure_scraper-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6d2bac985358de99ea9b7948e132921ce8d00b52d774fa487e8ea7b5e633be3
MD5 3309c63afbbca4e090bf5a74fff21167
BLAKE2b-256 2db122c9b12083781cf6d25d8a8920b31dd34b4df34e24294c3b3d2e9fbb7c52

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