toapi: Declaratively Turn Any Website into a JSON API

This repository profile is provided by osrepos.com, an open source repository discovery platform.

toapi: Declaratively Turn Any Website into a JSON API

Summary

toapi is a powerful Python library designed to transform any website into a clean JSON API declaratively. It enables users to define desired data fields using CSS selectors, fetching and parsing web pages on demand. With built-in caching and support for dynamic content, toapi simplifies web data extraction without complex crawlers or databases.

Repository Information

Analyzed by OSRepos on July 24, 2026

Topics

Click on any tag to explore related repositories

Use at your own risk

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.

Introduction

toapi is a powerful Python library designed to declaratively turn any website into a clean JSON API. It simplifies the process of extracting data from web pages by allowing you to define the fields you want using standard CSS selectors. With toapi, pages are fetched and parsed on demand, complete with built-in caching, eliminating the need for complex web crawlers or database maintenance. The core idea behind toapi is that "Every web site provides APIs," and it helps you unlock them.

Installation

Getting started with toapi is straightforward. You can install it using pip:

pip install toapi

Please note that toapi requires Python 3.10 or higher.

Examples

Here's a quick example demonstrating how to use toapi to create an API for Hacker News posts:

from htmlparsing import Attr, Text
from toapi import Api, Item

api = Api()


@api.site("https://news.ycombinator.com")
@api.list(".athing")
@api.route("/posts", "/news")
@api.route("/posts?page={page}", "/news?p={page}")
class Post(Item):
    title = Text(".titleline > a")
    url = Attr(".titleline > a", "href")


api.run(host="127.0.0.1", port=5000)

After running this Python script, you can visit http://127.0.0.1:5000/posts in your browser to get a JSON output similar to this:

{
  "Post": [
    {"title": "Mathematicians Crack the Cursed Curve", "url": "https://www.quantamagazine.org/..."},
    {"title": "Stuffing a Tesla Drivetrain into a 1981 Honda Accord", "url": "https://jalopnik.com/..."}
  ]
}

This example showcases how easily you can define an API endpoint, specify the target website, and extract specific elements using CSS selectors.

Why Use toapi?

toapi offers several compelling features that make it an excellent choice for web data extraction:

  • Declarative: Describe the data you want, not the scraping logic, leading to cleaner and more maintainable code.
  • Routes: Map clean API paths to messy source URLs, supporting {param} placeholders for dynamic content.
  • Multi-site Support: Merge data from several different websites behind a single, unified API.
  • Cleaning Hooks: Define clean_<field> methods on your Item classes to post-process and transform extracted values before they are returned.
  • Automatic Caching: Pages and parsed results are automatically cached, improving performance and reducing requests to the source website.
  • Headless Browser Support: Easily integrate a headless browser (e.g., via Api(browser="/path/to/geckodriver")) for scraping JavaScript-heavy sites.

Links

Related repositories

Similar repositories that may be relevant next.

dogpile.cache: A Robust Python Caching API for Various Backends

dogpile.cache: A Robust Python Caching API for Various Backends

May 9, 2026

dogpile.cache is a Python caching API designed to offer a generic interface to diverse caching backends. It builds upon the "dogpile lock" concept, ensuring efficient resource creation while allowing other threads to access previous versions. This project serves as a modern, more efficient replacement for the Beaker caching system, developed by the same author.

pythoncachingapi
Graphene: A Powerful GraphQL Framework for Python

Graphene: A Powerful GraphQL Framework for Python

May 4, 2026

Graphene is an opinionated Python library designed for building GraphQL schemas and types quickly and easily. It offers built-in support for Relay, is data-agnostic, and integrates seamlessly with various frameworks like Django and SQLAlchemy. This framework simplifies the process of exposing your data through a GraphQL API in Python applications.

frameworkgraphenegraphql
Fern: Generate Type-Safe SDKs and API Documentation from OpenAPI

Fern: Generate Type-Safe SDKs and API Documentation from OpenAPI

March 10, 2026

Fern is a powerful platform that streamlines API development by transforming your API definitions into production-ready SDKs and comprehensive documentation. It supports various API specifications, including OpenAPI, and offers multi-language SDKs, interactive developer documentation, and AI-powered search. This tool significantly enhances the developer experience by automating crucial parts of the API lifecycle.

apidocs-as-codedocs-generator
langcorn: Serve LangChain LLM Apps and Agents with FastAPI

langcorn: Serve LangChain LLM Apps and Agents with FastAPI

March 2, 2026

Langcorn is an innovative API server designed to effortlessly deploy LangChain models and pipelines. It leverages the high-performance FastAPI framework, offering a robust and scalable solution for serving large language model applications. With features like easy installation, built-in authentication, and support for custom API keys, Langcorn streamlines the process of bringing your LLM projects to production.

apifastapilangchain

Source repository

Open the original repository on GitHub.

View on GitHub
OS
OSRepos

Analysis and discovery of open source repositories. Find interesting projects and follow their updates.

Monitor your website with YourWebsiteScore

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of third-party repository code is at your own risk. Always review source code, dependencies, licenses, and security implications before running anything.

© 2025 OSRepos. Built with Nuxt 3 and lots of ❤️