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
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With