search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Finding elements by tag name in Beautiful Soup

Beautiful Soup
chevron_right
Cookbooks
chevron_right
Finding elements Cookbook
schedule Jul 1, 2022
Last updated
local_offer PythonBeautiful Soup
Tags

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>
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...