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

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

html_form_to_dict-2021.11.3-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file html_form_to_dict-2021.11.3-py3-none-any.whl.

File metadata

  • Download URL: html_form_to_dict-2021.11.3-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for html_form_to_dict-2021.11.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cda22bedd4416cb7116ed89c04f0c81d5dff9cbae0f7cc1c688024ea1267a64b
MD5 cacc0f365e57b18e2c0cd7a10cb3c036
BLAKE2b-256 0f5f89c1d7e853c9d340ad13a3695ddd5a8d47416432e89ec875010906c618cc

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