syntax

1
2
#find h2 tag
print(soup.h2)
1
2
#find all 'h2' tags
soup.find_all('h2')

example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from bs4 import BeautifulSoup
#html source
html_source = '''
<div id="test">
     <h2>paragraph1</h2>
</div>
'''

soup = BeautifulSoup(html_source, 'html.parser')
#find h2 tag
print(soup.h2)

output

1
<h2>paragraph1</h2> 

find all example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from bs4 import BeautifulSoup
#html source
html_source = '''
<div id="test">
     <h2>paragraph1</h2>
  </div>

  <div id="test">
     <h2>paragraph2</h2>
  </div>

  <div id="test">
     <h2>paragraph3</h2>
  </div>

  <div id="test">
     <h2>paragraph4</h2>
  </div> 

  <div id="test">
     <h2>paragraph5</h2>
  </div>

  <div id="test">
     <h2>paragraph6</h2>
  </div>
'''

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



#find all 'h2' tags
find_all = soup.find_all('h2')
print(find_all)   

outout

1
[<h2>paragraph1</h2>, <h2>paragraph2</h2>, <h2>paragraph3</h2>, <h2>paragraph4</h2>, <h2>paragraph5</h2>, <h2>paragraph6</h2>]        

English today is not an art to be mastered it's just a tool to use to get a result