Last modified: February 21, 2020

Find elements by ID python BeautifulSoup

In this tutorial, we'll learn how to find elements by attribute id using BeautifulSoup.

1. Finding all H2 elements by Id

Syntax


soup.find_all(id='Id value')

Example

in the following example, we'll find all elements that have "test" as ID value.


from bs4 import BeautifulSoup

#html source
html = '''
    <div id="test">
     <h2>hello world1</h2>
  </div>

  <div id="test">
     <h2>hello world2</h2>
  </div>

  <div id="test">
     <h2>hello world3</h2>
  </div>

  <div id="test">
     <h2>hello world4</h2>
  </div> 

  <div id="test">
     <h2>hello world5</h2>
  </div>

  <div id="test">
     <h2>hello world6</h2>
  </div>
''' 

soup = BeautifulSoup(html)

#find elements
find_all_id = soup.find_all(id='test')

#print elements
print(find_all_id)

Output


[<div id="test">                                                                                                                       
<h2>hello world1</h2>                                                                                                                  
</div>, <div id="test">                                                                                                                
<h2>hello world2</h2>                                                                                                                  
</div>, <div id="test">                                                                                                                
<h2>hello world3</h2>                                                                                                                  
</div>, <div id="test">                                                                                                                
<h2>hello world4</h2>                                                                                                                  
</div>, <div id="test">                                                                                                                
<h2>hello world5</h2>                                                                                                                  
</div>, <div id="test">                                                                                                                
<h2>hello world6</h2>                                                                                                                  
</div>] 


2. getting H2's value

After getting the result, let's now get H2's tag value.


#getting h2 value
for i in find_all_id:
    print(i.h2.string)

output


hello world1                                                                                                                          
hello world2                                                                                                                          
hello world3                                                                                                                          
hello world4                                                                                                                          
hello world5                                                                                                                          
hello world6