palbha nazwale
palbha nazwale

Reputation: 11

Particular class element in a beatutiful soup

i have below html text <div class="jtxt" title="abcd"></div> <div class="jtxt orange"> <span>Confidential</span>
' i want to print only abcd i.e part of class=jtxt and not include jtxt jco when i print a.findAll("div",{"class":"jtxt"}) all title is getting printed . i don't want any restirction based on index where is it getting printed . Is there any way to restrict class="jtxt" not containing "jtxt" .
I am doing above code in python using beautiful soup

Upvotes: 0

Views: 27

Answers (2)

chitown88
chitown88

Reputation: 28565

import bs4

html = '''<div class="jtxt" title="abcd"></div> <div class="jtxt orange">  <span>Confidential</span>'''

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

title = soup.find('div')['title']

print (title)

Output:

'abcd'

Upvotes: 0

Bitto
Bitto

Reputation: 8205

You can access a tag’s attributes by treating the tag like a dictionary. You can read more about this in the documentation.

from bs4 import BeautifulSoup
html="""
<div class="jtxt" title="abcd"></div> <div class="jtxt orange">  <span>Confidential</span>
"""
soup=BeautifulSoup(html,'html.parser')
print(soup.find('div',class_='jtxt')['title'])

Output

abcd

Upvotes: 1

Related Questions