Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting content from last element using BeautifulSoup find_all

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

like image 704
66Mhz Avatar asked Aug 26 '14 02:08

66Mhz


People also ask

What does Find_all return BeautifulSoup?

find_all returns an object of ResultSet which offers index based access to the result of found occurrences and can be printed using a for loop.

What is the difference between Find_all () and find () in BeautifulSoup?

find is used for returning the result when the searched element is found on the page. find_all is used for returning all the matches after scanning the entire document.

How do I find a specific element with BeautifulSoup?

BeautifulSoup has a limited support for CSS selectors, but covers most commonly used ones. Use select() method to find multiple elements and select_one() to find a single element.


2 Answers

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
like image 59
Padraic Cunningham Avatar answered Nov 15 '22 10:11

Padraic Cunningham


last_div = None
for last_div in post_content:pass
if last_div:
    content = last_div.getText()

And then you get the last item of post_content.

like image 32
atupal Avatar answered Nov 15 '22 08:11

atupal