search
Search
Unlock 100+ guides
search toc
close
account_circle
Profile
exit_to_app
Sign out
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
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
near_me
Linear Algebra
54 guides
keyboard_arrow_down
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

# 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 BeautifulSoupsoup = 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 BeautifulSoupsoup = 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> ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!