search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Beautiful Soup | find_all method

schedule Aug 12, 2023
Last updated
local_offer
PythonBeautiful Soup
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Beautiful Soup's find_all(~) method returns a list of all the tags or strings that match a particular criteria.

Parameters

1. namelink | string | optional

The name of the tag to return.

2. attrslink | string | optional

The tag attribute to filter for.

3. recursivelink | boolean | optional

Boolean indicating whether to look through all descendants of the tag. Defaults to recursive=True.

4. stringlink | string | optional

The string to search for (rather than tag).

5. limitlink | number | optional

The number of elements to return. Defaults to all matching.

Examples

Consider the following HTML document:

my_html = """
<div>
    <p id="alex">Alex</p>
    <p class="Bob">Bob</p>
    <p id="cathy">Cathy</p>
</div>
"""
soup = BeautifulSoup(my_html, "html.parser")

Find by Tag Name

To return a list of all the <p> tags:

soup.find_all("p")
[<p id="alex">Alex</p>, <p class="Bob">Bob</p>, <p id="cathy">Cathy</p>]

Find by Attribute

To find all tags with id="cathy":

soup.find_all(id="cathy")
[<p id="cathy">Cathy</p>]

Find by Class

To find all tags with class="Bob":

soup.find_all(class_="Bob")
[<p class="Bob">Bob</p>]
NOTE

Notice how we have to use class_ rather than class as it is a reserved word in Python.

Recursive

Consider the following HTML:

my_html = """
   <div id="people">
      <p>Alex</p>
      <div>
         <p>Bob</p>
         <p>Cathy</p>
      </div>
   <div>
"""

soup = BeautifulSoup(my_html)

To recursively look for <p> tags under the <div id="people">:

soup.find(id="people").find_all("p")
[<p>Alex</p>, <p>Bob</p>, <p>Cathy</p>]

To only look for <p> tags directly under the <div id="people"> tag:

soup.find(id="people").find_all("p", recursive=False)
[<p>Alex</p>]

Note that only the <p> tag that is a child of the <div id="people"> tag is returned.

Find by String

Reminder, here is the HTML we are working with:

my_html = """
<div>
    <p id="alex">Alex</p>
    <p class="Bob">Bob</p>
    <p id="cathy">Cathy</p>
</div>
"""
soup = BeautifulSoup(my_html, "html.parser")

To find all the strings "Alex" and "Cathy":

soup.find_all(string=["Alex", "Cathy"])
['Alex', 'Cathy']

Limit

To limit the number of returned results to 2:

soup.find_all("p", limit=2)
[<p id="alex">Alex</p>, <p class="Bob">Bob</p>]

Note how we only return the first two <p> tags.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
9
thumb_down
1
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!