Last modified: Apr 01, 2023 By Alexander Williams

BeautifulSoup find strong tag [Examples]

Find all strong tags

Syntax:

soup.find_all('strong')

Example:

# Import bs4 module
from bs4 import BeautifulSoup

# HTML document as a string
html_doc = """
<html>
<head>
<title>Example HTML Document</title>
</head>
<body>
<p>This is an example paragraph with a <strong>strong tag</strong> inside it.</p>
<p>Another paragraph with a <strong>strong tag</strong>.</p>
</body>
</html>
"""

# Create a BeautifulSoup object
soup = BeautifulSoup(html_doc, 'html.parser')

# Use the find_all method to find all <strong> tags in the document
strong_tags = soup.find_all('strong')

# Loop through the strong_tags list and print each tag
for tag in strong_tags:
    print(tag)

Output:

<strong>strong tag</strong>
<strong>strong tag</strong>

Find the first strong tag

Syntax:

soup.find('strong')

Example:

# Import the BeautifulSoup
from bs4 import BeautifulSoup

# Define an HTML document as a string
html_doc = """
<html>
<head>
<title>Example HTML Document</title>
</head>
<body>
<p>This is an example paragraph with a <strong>strong tag</strong> inside it.</p>
<p>Another paragraph with a <strong>strong tag</strong>.</p>
</body>
</html>
"""

# Create a BeautifulSoup object
soup = BeautifulSoup(html_doc, 'html.parser')

# Use the find method to find the first <strong> tag in the document
first_strong_tag = soup.find('strong')

# Print the first_strong_tag variable, which contains the first <strong> tag
print(first_strong_tag)

Output:

<strong>strong tag</strong>

Get the text of a strong tag

Syntax:

tag.text

Example:

# Import the BeautifulSoup class
from bs4 import BeautifulSoup

# Define an HTML document as a string
html_doc = """
<html>
<head>
<title>Example HTML Document</title>
</head>
<body>
<p>This is an example paragraph with a <strong>strong tag</strong> inside it.</p>
<p>Another paragraph with a <strong>strong tag</strong>.</p>
</body>
</html>
"""

# Create a BeautifulSoup object
soup = BeautifulSoup(html_doc, 'html.parser')

# Use the find method to find the first <strong> tag in the document
first_strong_tag = soup.find('strong')

# Use the .text attribute to get the text of the <strong> tag
strong_text = first_strong_tag.text

# Print the strong_text variable, which contains the text of the <strong> tag
print(strong_text)

Output:

strong tag

Get the text of a all strong tag

Example:

from bs4 import BeautifulSoup

html_doc = """
<html>
<head>
<title>Example HTML Document</title>
</head>
<body>
<p>This is an example paragraph with a <strong>strong tag</strong> inside it.</p>
<p>Another paragraph with a <strong>strong tag</strong>.</p>
</body>
</html>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

# Use the find_all method to find all <strong> tags in the document
strong_tags = soup.find_all('strong')

# Loop through each <strong> tag and print its text
for strong_tag in strong_tags:
    print(strong_tag.text)

Output:

strong tag
strong tag