I would like to create a html form to edit some database tables.
I tried to create a form with a table where in each table row is one entry of the database table like this:
name: George | age: | ||||||||||
name: Jake | age: |
But I cannot get the values into a dictionary or pandas dataframe so I can loop over it and update the table. It would even by possible to just get a list of the id's and a list of the ages and then loop over them (if they stay in the correct order).
The table in the database has the structure id, name, age.
Then need to set or adjust the age for all rows, so I want to update the ages by the id.
解决方案
You can use python-form-parser
from lxml import etree
from urllib import urlencode
html_string = """
"""
# parse and extract the form
tree = etree.HTML(html_string)
form = tree.xpath("//form[@id='my_form']")[0]
# Extract default values and meta information
ff = FormFiller(form)
# click to 'my_submit' button: will return dictionary with all fields values
form_data = ff.click('my_submit')
print form_data
{"my_field": "my_value",
"my_submit": "Submit the form"}
# fill user data
form_data['some_name'] = 'some_value'
# prefered way on how to fill user data:
print ff.fill('my_submit', my_field2="my_value2")
{"my_field": "my_value",
"my_field2": "my_value2",
"my_submit": "Submit the form"}
# fields that not present on the original form are not allowed
ff.fill('my_submit', field_not_present_in_form_name="my_value3")
KeyError: 'Invalid form field'
# prepare data for sending
body = urlencode(form_data)
url = ff.get_action_url("http://example.com/forms/my_form.html")
print url
'http://example.com/forms/my_path' # my_path from form's "action" attribute
method = ff.get_method()
print method
'POST' # from form's "method" attr (GET is default)