Forms

input name and value from a form tag.

A <form> holds <input> tags. Read name and value (or the value attribute default). Buttons are inputs too — skip them if you only want fields.

Goal

Dump every named field from a small order form.

Inputs

html = """
<form id="order" action="/order" method="post">
  <input name="city" value="Nairobi" />
  <input name="units" type="number" value="2" />
  <button type="submit">Order</button>
</form>
"""
soup = BeautifulSoup(html, "html.parser")
form = soup.find("form", id="order")
print("action", form.get("action"), "method", form.get("method"))
for inp in form.find_all("input"):
    print(inp.get("name"), inp.get("type"), inp.get("value"))

As a dict

html = """
<form>
  <input name="city" value="Kisumu" />
  <input name="units" value="3" />
</form>
"""
soup = BeautifulSoup(html, "html.parser")
fields = {}
for inp in soup.select("form input[name]"):
    fields[inp.get("name")] = inp.get("value")
print(fields)

This is the default HTML, not a live POST. Filling a form in a real browser is a different tool.

Tip

<select> options use option[selected] or the first option. <textarea> text is tag.get_text(), not a value attribute.