我想创建一个html表单来编辑一些mysql表。
我尝试创建一个带有表的表单,其中每个表行是数据库表的一个条目,如下所示:
NameError: name '__gi__' is not defined
但我无法将值放入字典或pandas数据帧中,因此我可以循环并更新mysql表。它甚至可以只获得一个id列表和一个年龄列表然后循环它们(如果它们保持正确的顺序)。
数据库中的表具有结构ID,名称,年龄。 然后需要设置或调整所有行的年龄,所以我想用id更新年龄。
答案 0 :(得分:0)
您可以使用 python-form-parser
from lxml import etree
from urllib import urlencode
html_string = """
<html>
<body>
<form action="my_path", method="POST" id="my_form">
<input type="text" name="my_field" value="my_value"/>
<input type="text" name="my_field2"/>
<input type="submit" name="my_submit" value="Submit the form"/>
</form>
</body>
</html>
"""
# 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)