smviswa
smviswa

Reputation: 111

How to find particular div text in Beautiful Soup

I have the following html:

from bs4 import BeautifulSoup as soup
html_doc = """<div class="__cast-member" content="Vishal Krishna Reddy" itemprop="name"><div class="__cast-image wow fadeIn"><meta content="https://in.bmscdn.com/iedb/artist/images/website/poster/large/vishal-krishna-reddy-16275-24-03-2017-15-17-54.jpg" itemprop="image"><img alt="Vishal Krishna Reddy" data-error="//in.bmscdn.com/webin/profile/user.jpg" data-lazy="//in.bmscdn.com/iedb/artist/images/website/poster/large/vishal-krishna-reddy-16275-24-03-2017-15-17-54.jpg" title="Vishal Krishna Reddy"/></meta></div><br/>Developer<br><span class="__role">Actor</span><br><span class="__characterName">As Kathiravan</span></br></br></div>"""
html = soup(html_doc, "html.parser")
Cast=html.find("div", {"class":"__cast-member"})
print Cast.text

output: DeveloperActorAs Kathiravan

But I need output for only : Developer

Upvotes: 1

Views: 2174

Answers (1)

Keyur Potdar
Keyur Potdar

Reputation: 7238

You can use the .next_sibling property to get the text you want. Find the <div> tag with class="__cast-image wow fadeIn" first. The text you want is located after this tag. So, use .next_sibling on this tag. But first you'll get <br/>, so use it again.

>>> soup.find('div', class_='__cast-image').next_sibling
<br/>
>>> soup.find('div', class_='__cast-image').next_sibling.next_sibling
'Developer'

Upvotes: 2

Related Questions