Skip to main content

Parse HTML to a Python Dictionary

Project description

Python package

HTML Form to Dict

This is a tiny library which provides a method called html_form_to_dict().

This method takes a string containing HTML and returns a dictionary of the value of the first form.

You can use it in tests like this:

def test_foo(client):
    url = reverse('foo')
    response = client.get(url)
    data = html_form_to_dict(response.content) # <====================
    assert data == {'city': 'Chemnitz', 'name': 'Mr. X'}
    data['name']='Mrs. Y'
    response = client.post(url, data)
    assert resonse.status == 302, response.context['form'].errors

Above code uses pytest-django. See client fixture

The dictionary returned by html_form_to_dict() does not allow adding new keys, which are not in the dictionary yet. This way you get an error if your test sets the value for an input which (due to refactoring) does not exist any more.

Install

pip install html_form_to_dict

Development

You need to upload your ssh-pub-key to github first:

pip install -e git+ssh://git@github.com/guettli/html_form_to_dict#egg=html_form_to_dict
edit-the-code
pytest
create Pull-Request

Alternatives

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

html_form_to_dict-2021.11.2-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

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