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.

The data returned by html_form_to_dict() is a FormDict which has the method submit(). This way you can submit the data like a real browser would.

The submit() method supports the "action" and "method" attributes of forms and additionaly the htmx attributes hx-get, hx-post.

Example:

def test_foo(client):
    ...
    
    # client is a DjangoClient. But you could use
    # python-requests or a different URL-lib, too
    response = client.get(url)
    
    # This method parses the HTML in response.content to a dictionary.
    # This dictionary is like request.POST or request.GET.
    # It is a flat mapping from the input elements of the form
    # to their value.
    data = html_form_to_dict(response.content)
    
    # Now you can test the default values of the form.
    assert data == {'city': 'Chemnitz', 'name': 'Mr. X'}
    
    # You can edit the data. This is like a human (or Playwright/Selenium)
    # altering the HTML input fields
    data['name'] = 'Mrs. Y'
    
    # This submits the data to the server.
    # This methods uses the "action" attribute of the form.
    # The hx-get, hx-post attributes of htmx are supported, too
    response = data.submit(client)
    
    # If you use the Post/Redirect/Get pattern:
    assert response.status == 302, response.context['form'].errors

Above code uses pytest-django. See client fixture

The FormDict 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 (maybe due to refactoring) does not exist.

Above example uses Django, but the library is a pure Python library which does not depend on any particular web-framework.

This library was build for testing, but you can use it for all tasks where you want to parse and submit html forms.

This library does not evaluate JavaScript. If you need JS support, please use Playwright (or a similar tool).

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
pip install pytest
pytest
create Pull-Request

Alternatives

Deploy

via deploy-library.py

for py2 tgz package: python -m twine upload dist/html_form_to_dict-*.tar.gz

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

html_form_to_dict-2022.5.4.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

html_form_to_dict-2022.5.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file html_form_to_dict-2022.5.4.tar.gz.

File metadata

  • Download URL: html_form_to_dict-2022.5.4.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for html_form_to_dict-2022.5.4.tar.gz
Algorithm Hash digest
SHA256 70881ad101d76981b838003e7a13cfedcc8ce24ffc5a7c07232a1fd32ecd38a1
MD5 08240e48ec09368319c1172ddff8bf11
BLAKE2b-256 815ecdd2517d1c852cfd52c5e08558f3afbff1b0229bed4f5c6b619655c9c9d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for html_form_to_dict-2022.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c2d564b61654504c7ce015455196f595071eb3abf1f7f8696a44ce24e5dbe76b
MD5 f8a73eedc566bbdef5de7dadd929ced2
BLAKE2b-256 facb61f9f3d558db7a25a0349872a332cf0650a9edf4391226de9b963772f10d

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