Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautiful Soup: extracting from deeply nested <div>'s

Trying to extract Message text from:

<div class="Item ItemDiscussion Role_Member" id="Discussion_2318">
<div class="Discussion">
<div class="Item-BodyWrap">
<div class="Item-Body">
<div class="Message">
                Hello<br/>I have a very interesting observation on nature of birds in Alaska ...  <br/>
 Was there  10/19/18 has anyone heard of this     </div>
<div class="ReactionRecord"></div><div class="Reactions"></div> </div>
</div>
</div>
</div>

I have got this bit with:

tag = soup.find('div', {'class' : 'ItemDiscussion'})

Next I am trying to go down with:

s = str((tag.contents)[1])
sp = BeautifulSoup(s)
sp.contents

But this does not help much. How to get message text from <div class="Message"> ?

like image 875
dokondr Avatar asked Nov 25 '25 21:11

dokondr


1 Answers

you can find the element from soup directly.

discussion_div = soup.find("div", {"class": "ItemDiscussion"})
message_text = discussion_div.find("div", {"class": "Message"}).text
like image 193
Manali Kagathara Avatar answered Nov 28 '25 11:11

Manali Kagathara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!