Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Difference Between '+' and 'append' in Python
Next article icon

Difference between find and find_all in BeautifulSoup - Python

Last Updated : 21 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

find() method

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 os from bs4 import BeautifulSoup as bs import os  # Remove the last segment of the path base=os.path.dirname(os.path.abspath(__file__))  # Open the HTML in which you want to # make changes html=open(os.path.join(base, 'gfg.html'))  # Parse HTML file in Beautiful Soup soup=bs(html, 'html.parser')  # Obtain the text from the widget after  # finding it find_example=soup.find("p", {"id":"vinayak"}).get_text()  # Printing the text obtained received  # in previous step print(find_example) 

Output:

find_all() method

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.

Syntax:

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 os from bs4 import BeautifulSoup as bs import os  # Remove the last segment of the path base=os.path.dirname(os.path.abspath(__file__))  # Open the HTML in which you want to  # make changes html=open(os.path.join(base, 'gfg.html'))  # Parse HTML file in Beautiful Soup soup=bs(html, 'html.parser')  # Construct a loop to find all the # p tags for 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:

Table of Difference between find and find_all

S.No.       

find

find_all

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)


Next Article
Difference Between '+' and 'append' in Python

V

vin8rai
Improve
Article Tags :
  • Python
  • Difference Between
  • Python BeautifulSoup
Practice Tags :
  • python

Similar Reads

  • Difference between Puppeteer and Beautifulsoup
    1. Puppeteer : Is a tool developed by Google for automating the browser. Puppeteer is very powerful and at the same time, it is very handy to use. Unlike beautifulsoup, It brings the whole browser engine API to work with enabling one to use a lot of advanced features and not just web scraping 2. Bea
    2 min read
  • Difference between 'and' and '&' in Python
    and is a Logical AND that returns True if both the operands are true whereas '&' is a bitwise operator in Python that acts on bits and performs bit-by-bit operations. Note: When an integer value is 0, it is considered as False otherwise True when used logically. and in PythonThe 'and' keyword in
    6 min read
  • Difference Between '+' and 'append' in Python
    + Operator creates a new sequence by concatenating two existing sequences and .append() method (available for lists) modifies the existing list in place by adding one item at the end. In this article we are going to compare '+' operator and append in Python. + Operator in Python+ operator is typical
    3 min read
  • Difference between BeautifulSoup and Scrapy crawler
    Web scraping is a technique to fetch data from websites. While surfing on the web, many websites don’t allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious and time-consuming. Web Scraping is the automation of the data extraction process from w
    3 min read
  • Difference between end and sep in Python
    In this article we will discuss the difference between The end and sep are two parameters in Python's built-in print() function that will help to control how the output is formatted. end in PythonThe end is a parameter in Python's built-in print() function that controls what character(s) are printed
    2 min read
  • Difference between dir() and vars() in Python
    As Python stores their instance variables in the form of dictionary belonging to the respective object both dir() and vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.dir(
    2 min read
  • BeautifulSoup - Find all children of an element
    You might have seen there are various websites that are complex as well as lengthy, from which searching anything becomes difficult. To ease our work of searching, modifying, and iteration, Python gives us some inbuilt libraries, such as Requests, Xml, Beautiful Soup, Selenium, Scrapy, etc. Among al
    3 min read
  • Python: Difference between dir() and help()
    In Python, the dir() and help() functions help programmers understand objects and their functionality. dir() lists all the attributes and methods available for an object, making it easy to explore what it can do.help() provides detailed information about an object, including descriptions of its meth
    5 min read
  • Difference between + and , Python Print
    In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as
    3 min read
  • Python BeautifulSoup - find all class
    Prerequisite:- Requests , BeautifulSoup The task is to write a program to find all the classes for a given Website URL. In Beautiful Soup there is no in-built method to find all classes. Module needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This modu
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences