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.4-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: html_form_to_dict-2021.11.4-py3-none-any.whl
  • Upload date:
  • Size: 5.4 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5c3986c86cb0ed17a1656b2195e93b59634151d3b7c0be82a899874e1e7585d6
MD5 072ee7fc1dd72a3cc687c0b0f48aacf0
BLAKE2b-256 4f290b2e1f221a6bb0f52476726942c6e951b5c8723ebaead381d5c0bb537a3e

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