syntax

1
p['class']

example

1
2
3
4
5
6
7
8
html_source = '''
    <div>
       <p class="test">hello world</p>
    </div>   
'''

soup = BeautifulSoup(html_source, 'html.parser')
print(soup.p['class'])

output

1
['test'] 

find all classes of p tags

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
html_source = '''
    <div>
       <p class="test1">hello world</p>
        <p class="test2"hello world</p>
         <p class="test3">hello world</p>
          <p class="test4">hello world</p>
    </div>   
'''

soup = BeautifulSoup(html_source, 'html.parser')
#find all p tags
findall = soup.find_all('p')

#loop and get class of p tag
for i in findall:
    print(i['class'])

output

1
2
3
4
['test1']                                                                                                                             
['test2']                                                                                                                             
['test3']                                                                                                                             
['test4'] 

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