Last modified: Jun 06, 2023 By Alexander Williams

Find First or All Span Tags Using BeautifulSoup

The <span> tag is an HTML element used to mark a specific section of text within a larger document or container.  In this tutorial, we'll learn how to find the first or all span tags using BeautifulSoup.

Finding the first Span tag

To find the first <span> tag in an HTML document using BeautifulSoup, you can use the following code:


from bs4 import BeautifulSoup

html_source = '''
<div>
    <span>hello</span>
</div>
'''

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

# Find the first <span> tag
first_span = soup.find('span')

# Check if a <span> tag was found
if first_span is not None:
    print(first_span)
else:
    print("No <span> tag found.")

Output:

<span>hello</span>

However, in this code, we've used the find() method to find the first occurrence of the <span> tag. Then, we've checked if first_span is not None, and if so, we print it. Otherwise, we print a message indicating that no <span> tag was found.

To print the content of the <span> tag, you can use the .text property of the first_span object. Here's the modified code to achieve that:

from bs4 import BeautifulSoup

html_source = '''
<div>
    <span>hello</span>
</div>
'''

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

# Find the first <span> tag
first_span = soup.find('span')

# Check if a <span> tag was found
if first_span is not None:
    print(first_span.text)
else:
    print("No <span> tag found.")

The output of this code will be:

hello

Finding all span tags

To find all <span> tags, you can use the .find_all() method. Here's the code:

from bs4 import BeautifulSoup

html_source = '''
<div>
    <span>Python</span>
    <span>Java</span>
    <span>PHP</span>
</div>
'''

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

# Find all <span> tags
span_tags = soup.find_all('span')

# Print each <span> tag
for span in span_tags:
    print(span)

Output:

<span>Python</span>
<span>Java</span>
<span>PHP</span>

The code above finds all the <span> tags within the HTML source and then iterate over them using a loop. Inside the loop, it prints each <span> tag.

The code below shows how to print the content of each <span> tag:

from bs4 import BeautifulSoup

html_source = '''
<div>
    <span>Python</span>
    <span>Java</span>
    <span>PHP</span>
</div>
'''

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

# Find all <span> tags
span_tags = soup.find_all('span')

# Print the content of each <span> tag
for span in span_tags:
    print(span.text)

Output:

Python
Java
PHP

Conclusion

The provided codes utilize the BeautifulSoup library to parse an HTML source and work with <span> tags. Overall, we've learned how to find <span> tags, access their content, and locate and print all occurrences of <span> tags in the given HTML source using BeautifulSoup.