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:[email protected] ">[email protected]</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.
CodePudding user response:
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 >
<span >Contact<br></span>
<div>Steven Cantrell</div>
<div >Department of Justice</div>
<div >Federal Bureau of Investigation</div>
<!---->
<!---->
<!---->
<div ><a href="mailto:[email protected] ">[email protected]</a></div>
<div >256-313-8835</div>
</div>'''
soup = BeautifulSoup(html, 'html.parser')
contact = soup.find(text='Contact').find_next('div').text
Output:
print(contact)
Steven Cantrell