Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python BeautifulSoup find all input for specific form

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?

like image 787
user1220022 Avatar asked Jun 19 '15 13:06

user1220022


1 Answers

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")
like image 109
alecxe Avatar answered Nov 14 '22 23:11

alecxe