Python – Getting content from last element using BeautifulSoup find_all

beautifulsouppython

I'm trying to extract the content from the last div in in a list created by find_all.

post_content = soup.find_all('div',{'class': 'body_content_inner'})

stores the following text:

[<div class="body_content_inner">
 post #1 content is here
 </div>, <div class="body_content_inner">
 post #2 content is here
 </div>]

I'd like to extract the text that is stored within the last div tag but I am unsure how to iterate through post_content

Best Solution

html = """
<div class="body_content_inner">
 post #1 content is here
 </div>, <div class="body_content_inner">
 post #2 content is here
 </div>
  """
soup = BeautifulSoup(html)
print soup.find_all("div")[-1].get_text()
post #2 content is here
Related Question