Parse HTML to a Python Dictionary
Project description
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
- Mechanize This library is like a browser without JS support.
- You could use BeautifulSoup like explained in this Stackoverflow Answer
- Use Playwright for browser based end-to-end tests.
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for html_form_to_dict-2021.11.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c3986c86cb0ed17a1656b2195e93b59634151d3b7c0be82a899874e1e7585d6 |
|
MD5 | 072ee7fc1dd72a3cc687c0b0f48aacf0 |
|
BLAKE2b-256 | 4f290b2e1f221a6bb0f52476726942c6e951b5c8723ebaead381d5c0bb537a3e |