python BeautifulSoup find all input for specific form

11,599

As noted in comments, chain find and find_all() for the context-specific search:

form = soup.find('form')
inputs = form.find_all('input')

If you want direct input elements only, add recursive=False:

form.find_all('input', recursive=False)

Or, using CSS selectors:

soup.select("form input")

And, getting direct input child elements only:

soup.select("form > input")
Share:
11,599
user1220022
Author by

user1220022

Updated on June 24, 2022

Comments

  • user1220022
    user1220022 almost 2 years

    I'm trying to use BeautifulSoup to extract input fields for a specific form only.

    Extracting the form using the following:

    soup.find('form')
    

    Now I want to extract all input fields which are a child to that form only.

    How can I do that with BS?