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

Finding elements by tag name in Beautiful Soup

schedule Aug 10, 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!

Extracting a single element by tag name

To extract a single element by tag name, use either the methods find(tag_name) or select_one(tag_name), both of which return the first occurrence of an element with the specified tag.

Example

Suppose we have the following html document:

my_html = """
   <html>
      <p>Alex</p>
      <p>Bob</p>
   </html>
"""

Using find method

To extract the first element with the p tag:

from bs4 import BeautifulSoup
soup = BeautifulSoup(my_html, "html.parser")
soup.find("p")
<p>Alex</p>

If there is no element with the specified tag, None is returned.

Using select_one method

To extract the first element with the p tag:

soup.select_one("p")
<p>Alex</p>

If there is no element with the specified tag, None is returned.

Using dot notation

Equivalently, we could also access the first element with the p tag like so:

soup.p
<p>Alex</p>

Extracting multiple elements by tag name using find_all method

To extract multiple elements by tag name, we could use either the methods find_all(tag_name) or the select(tag_name), both of which return a list of elements with the specified tag.

Example

Suppose we have the following html document:

my_html = """
   <html>
      <p>Alex</p>
      <p>Bob</p>
      <p>Cathy</p>
   </html>
"""

Using find_all method

To extract all elements wit the p tag:

from bs4 import BeautifulSoup
soup = BeautifulSoup(my_html, "html.parser")

for item in soup.find_all("p"):
   print(item)
<p>Alex</p>
<p>Bob</p>
<p>Cathy</p>

Since the find_all(~) method is so commonly used, there is a handy shorter-form that is equivalent:

for item in soup("p"):
   print(item)
<p>Alex</p>
<p>Bob</p>
<p>Cathy</p>

If there is no element with the specified tag, an empty list is returned.

Using select method

To extract all elements with the p tag using the select(~) method:

for item in soup.select("p"):
   print(item)
<p>Alex</p>
<p>Bob</p>
<p>Cathy</p>
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...