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

Viewed 58

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"> ?

2 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

You can select any element using select_one() function by entering the CSS Selector to the element. select_one() function will only return one element if you want more than one element then you can use select() which will return a list of found elements. here is the example for you,

soup = BeautifulSoup(html, "html.parser")
print soup.select_one("div.Item div.Discussion div.Item-BodyWrap div.Item-Body div.Message").text

You can also select your element using a single class if it is unique.

print soup.select_one("div.Message").text
Related