Skip to main content

Making parsing concise

Project description

A way to parse html fluently and as declarative as possible. This is in alpha stage now, things will change.

Installation

pip install parsechain

Usage

import requests
from parsechain import C, Response

# Fetch html and cast it
response = Response.cast(requests.get(...))

# Get a movie title and rating
title = response.css('h1 .title').text
rating = response.css('.left-box').inner_text.re(r'IMDb: ([\d.]+)').float

# Or both
movie = response.root.multi({
    'title': C.css('h1 .title').text,
    'rating': C.css('.left-box').inner_text.re(r'IMDb: ([\d.]+)').float,
})

The last example could be extended to show chains reuse:

def by_label(label):
    return C.css('.left-box').inner_text.re(fr'{label}: ([\w.]+)')

parse_movie = C.multi({
    'title': C.css('h1 .title').text,
    'rating': by_label('IMDb').float,
    'status': by_label('Status').trim,
})

movie = parse_movie(response.root)  # Pass a root of a tree

The complete list of available ops could be seen in parsechain.chains.Ops class. Proper documentation to follow, some day ;)

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

parsechain-0.0.3.tar.gz (8.0 kB view hashes)

Uploaded Source

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