Difference between find and find_all in BeautifulSoup – Python

21 Apr, 2021
BeautifulSoup is one of the most common libraries in Python which is used for navigating, searching, and pulling out data from HTML or XML webpages. The most common methods used for finding anything on the webpage are find() and find_all(). However, there is a slight difference between these two, let’s discuss them in detail.
The find method is used for finding out the first tag with the specified name or id and returning an object of type bs4.
Syntax: find_syntax=soup.find(“#Widget Name”, {“id”:”#Id name of widget in which you want to edit”}).get_text()
Example:
For instance, consider this simple HTML webpage having different paragraph tags.
HTML
<!DOCTYPE html><html>     <head>   Geeks For Geeks </head>     <body> <div>     <p id="vinayak">King</p>       <p id="vinayak1">Prince</p>       <p id="vinayak2">Queen</p>   </div> <p id="vinayak3">Princess</p>    </body>  </html>
For obtaining the text King, we use find method.
Python
# Find example  # Import the libraries BeautifulSoup# and osfrom bs4 import BeautifulSoup as bsimport os  # Remove the last segment of the pathbase=os.path.dirname(os.path.abspath(__file__))  # Open the HTML in which you want to# make changeshtml=open(os.path.join(base, 'gfg.html'))  # Parse HTML file in Beautiful Soupsoup=bs(html, 'html.parser')  # Obtain the text from the widget after # finding itfind_example=soup.find("p", {"id":"vinayak"}).get_text()  # Printing the text obtained received # in previous stepprint(find_example)
Output:
The find_all method is used for finding out all tags with the specified tag name or id and returning them as a list of type bs4.
for word in soup.find_all(‘id’):
     find_all_syntax=word.get_text()
     print(find_all_syntax)
Example:
For instance, consider this simple HTML webpage having different paragraph tags.
HTML
<!DOCTYPE html><html>     <head>   Geeks For Geeks </head>     <body> <div>     <p id="vinayak">King</p>       <p id="vinayak1">Prince</p>       <p id="vinayak2">Queen</p>   </div> <p id="vinayak3">Princess</p>    </body>    </html>
For obtaining all the text, i.e., King, Prince, Queen, Princess, we use find_all method.
Python
# find_all example  # Import the libraries BeautifulSoup# and osfrom bs4 import BeautifulSoup as bsimport os  # Remove the last segment of the pathbase=os.path.dirname(os.path.abspath(__file__))  # Open the HTML in which you want to # make changeshtml=open(os.path.join(base, 'gfg.html'))  # Parse HTML file in Beautiful Soupsoup=bs(html, 'html.parser')  # Construct a loop to find all the# p tagsfor word in soup.find_all('p'):      # Obtain the text from the received    # tags    find_all_example=word.get_text()      # Print the text obtained received     # in previous step    print(find_all_example)
Output:
1
find is used for returning the result when the searched element is found on the page. 
find_all is used for returning all the matches after scanning the entire document.
2
It is used for getting merely the first tag of the incoming HTML object for which condition is satisfied.  
It is used for getting all the incoming HTML objects for which condition is satisfied.  
3
The return type of find is <class ‘bs4.element.Tag’>.
The return type of find_all is <class ‘bs4.element.ResultSet’>
4
We can print only the first search as an output.
We can print any search, I.e., second, third, last, etc. or all the searches as an output.
5
Prototype: find(tag, attributes, recursive, text, keywords)
Prototype: findAll(tag, attributes, recursive, text, limit, keywords)
Picked
Python BeautifulSoup
Difference Between
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM
Stack vs Heap Memory Allocation
Difference between Process and Thread
Read JSON file using Python
Python map() function
Adding new column to existing DataFrame in Pandas
Python Dictionary
How to get column names in Pandas dataframe