Atom Store
Atom Store

Reputation: 1006

How to get the text of the next tag? (Beautiful Soup)

The html code is :

<div class="card border p-3">
               <span class="small text-muted">Contact<br></span>
               <div>Steven Cantrell</div>
               <div class="small">Department of Justice</div>
               <div class="small">Federal Bureau of Investigation</div>
               <!---->
               <!---->
               <!---->
               <div class="small"><a href="mailto:skcantrell@fbi.gov ">skcantrell@fbi.gov</a></div>
               <div class="small">256-313-8835</div>
            </div>

I want to get the output inside the <div> tag i.e. Steven Cantrell .

I need such a way that I should be able to get the contents of next tag. In this case, it is 'span',{'class':'small text-muted'}

What I tried is :

rfq_name = soup.find('span',{'class':'small text-muted'})
print(rfq_name.next)

But this printed Contact instead of the name.

Upvotes: 0

Views: 906

Answers (1)

chitown88
chitown88

Reputation: 28640

You're nearly there, just change your print to: print(rfq_name.find_next('div').text)

Find the element that has the text "Contact". Then use .find_next() to get the next <div> tag.

from bs4 import BeautifulSoup

html = '''<div class="card border p-3">
               <span class="small text-muted">Contact<br></span>
               <div>Steven Cantrell</div>
               <div class="small">Department of Justice</div>
               <div class="small">Federal Bureau of Investigation</div>
               <!---->
               <!---->
               <!---->
               <div class="small"><a href="mailto:skcantrell@fbi.gov ">skcantrell@fbi.gov</a></div>
               <div class="small">256-313-8835</div>
            </div>'''
            
soup = BeautifulSoup(html, 'html.parser')
contact = soup.find(text='Contact').find_next('div').text

Output:

print(contact)
Steven Cantrell

Upvotes: 2

Related Questions