Skip to main content

A library for converting any text (xml, html, plain text, stdout, etc) to python datatypes

Project description

Hatch project Documentation Status CI

Scrape-schema

This library is designed to write structured, readable, reusable parsers for unstructured text data (like html, stdout or any text) and is inspired by dataclasses

Motivation

Simplifying parsers support, where it is difficult to use or the complete absence of the API interfaces and decrease lines of code

Also structuring, data serialization and use as an intermediate layer for third-party serialization libraries: json, dataclasses, pydantic, etc


Features

  • python 3.8+ support
  • decrease lines of code for your parsers
  • partial support type-casting from annotations (str, int, float, bool, list, dict, Optional)
  • interacting with values with callbacks, filters, factories
  • logging to quickly find problems in extracted values
  • optional success-attempts parse values checker from fields objects
  • standardization, modularity* of structures-parsers *If you usage schema-structures and they are separated from the logic of getting the text (stdout output, HTTP requests, etc)

Build-in libraries parsers support:

  • re
  • bs4
  • selectolax(Modest)
  • parsel
  • lxml
  • selenium
  • playwright

Install

zero dependencies: regex, nested fields (and typing_extension if python < 3.11)

pip install scrape-schema

add bs4 fields

pip install scrape-schema[bs4]

add selectolax fields

pip install scrape-schema[selectolax]

add all fields

pip install scrape-schema[all]

Code comparison

Before scrape_schema: harder to maintain, change logic

import re
import pprint

TEXT = """
banana potato BANANA POTATO
-foo:10
-bar:20
lorem upsum dolor
192.168.0.1
"""


def parse_text(text: str) -> dict:
    if match := re.search(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", text):
        ipv4 = match[1]
    else:
        ipv4 = None

    if matches := re.findall(r"(\d+)", text):
        max_digit = max(int(i) for i in matches)
    else:
        max_digit = None

    failed_value = bool(re.search(r"(ora)", text))

    if matches := re.findall(r"(\d+)", text):
        digits = [int(i) for i in matches]
        digits_float = [float(f'{i}.5') for i in matches]
    else:
        digits = None
        digits_float = None
    words_lower = matches if (matches := re.findall(r"([a-z]+)", text)) else None
    words_upper = matches if (matches := re.findall(r"([A-Z]+)", text)) else None

    return dict(ipv4=ipv4, max_digit=max_digit, failed_value=failed_value,
                digits=digits, digits_float=digits_float, 
                words_lower=words_lower, words_upper=words_upper)
    

if __name__ == '__main__':
    pprint.pprint(parse_text(TEXT), width=48, compact=True)
    # {'digits': [10, 20, 192, 168, 0, 1],
    #  'digits_float': [10.5, 20.5, 192.5, 168.5, 0.5,
    #                   1.5],
    #  'failed_value': False,
    #  'ip_v4': '192.168.0.1',
    #  'max_digit': 192,
    #  'words_lower': ['banana', 'potato', 'foo',
    #                  'bar', 'lorem', 'upsum',
    #                  'dolor'],
    #  'words_upper': ['BANANA', 'POTATO']}

After scrape_schema: easy change of logic, support, portability

from typing import Annotated
import pprint

from scrape_schema import BaseSchema
from scrape_schema.fields.regex import ReMatch, ReMatchList

TEXT = """
banana potato BANANA POTATO
-foo:10
-bar:20
lorem upsum dolor
192.168.0.1
"""


class Schema(BaseSchema):
    ipv4: Annotated[str, ReMatch(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")]
    max_digit: Annotated[int, ReMatchList(r"(\d+)",
                                          callback=int,                                      
                                          factory=max)]
    failed_value: Annotated[bool, ReMatchList(r"(ora)", default=False)]
    digits: Annotated[list[int], ReMatchList(r"(\d+)")]
    digits_float: Annotated[list[float], ReMatchList(r"(\d+)", 
                                                     callback=lambda s: f"{s}.5")]
    words_lower: Annotated[list[str], ReMatchList(r"([a-z]+)")]
    words_upper: Annotated[list[str], ReMatchList(r"([A-Z]+)")]
    
if __name__ == '__main__':
    schema = Schema(TEXT)
    pprint.pprint(schema.dict(), width=48, compact=True)
    # {'digits': [10, 20, 192, 168, 0, 1],
    #  'digits_float': [10.5, 20.5, 192.5, 168.5, 0.5,
    #                   1.5],
    #  'failed_value': False,
    #  'ip_v4': '192.168.0.1',
    #  'max_digit': 192,
    #  'words_lower': ['banana', 'potato', 'foo',
    #                  'bar', 'lorem', 'upsum',
    #                  'dolor'],
    #  'words_upper': ['BANANA', 'POTATO']}

logging

In this project, logging to the DEBUG level is enabled by default.

To set up logger, you can get it by the name "scrape_schema"

import logging

logger = logging.getLogger("scrape_schema")
logger.setLevel(logging.INFO)
...

See more examples and documentation for get more information/examples


This project is licensed under the terms of the MIT license.

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

scrape_schema-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

scrape_schema-0.1.0-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: scrape_schema-0.1.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.0

File hashes

Hashes for scrape_schema-0.1.0.tar.gz
Algorithm Hash digest
SHA256 719550748de08baef8c4229ac2ab5128a971ca23dbd69536245782f844475356
MD5 3f6fa85defc224e91e514fd4ffca07cd
BLAKE2b-256 35301fb6aa3be47e201e32cc72e3691e4b9f4ace5039c4eebbe37b4a2c395b4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for scrape_schema-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90216d5ce2ab04570e85fc72d91c4acde3b46c14ecf8aed5cd6ac69289832ce0
MD5 e202075c720cd45908609435a8daa17d
BLAKE2b-256 ac31cc35e93fae50b66029b448346401cc84c4f158446ea9a0eac80ebf071b07

See more details on using hashes here.

Supported by

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