BeautifulSoup.find_all for nested divs without class attribute
<p>I am working with python2 and I wanted to get the content of a div in html page.</p>

<pre><code>&lt;div class="lts-txt2"&gt; 
   Some Content 
&lt;/div&gt;
</code></pre>

<p>If the div class is like above then I can get the content using</p>

<pre><code>BeautifulSoup.find_all('div', attrs={"class": 'lts-txt2'})
</code></pre>

<p>But if the div is like,</p>

<pre><code>&lt;div class="lts-txt2"&gt;
   &lt;div align="justify"&gt;
      Some Content 
   &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>then using </p>

<pre><code>BeautifulSoup.find_all('div', attrs={"class": 'lts-txt2'})
</code></pre>

<p>isn't return the content.
So I tried with</p>

<pre><code>BeautifulSoup.find_all('div', attrs={"align": 'justify'})
</code></pre>

<p>But it also wasn't worked.
How can I solve the problem.</p>

<p>You can extract all text from the node <em>including nested nodes</em> with the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#get-text" rel="nofollow"><code>Element.get_text()</code> method</a>:</p>

<pre><code>[el.get_text() for el in soup.find_all('div', attrs={"class": 'lts-txt2'})]
</code></pre>

<p>This produces a list with the textual content of each such a <code>div</code>, wether or not there is a nested <code>div</code> inside.</p>

<p>You could also use the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors" rel="nofollow">CSS selector <code>Element.select()</code> function</a> to select the nested div:</p>

<pre><code>soup.select('div.lts-txt2 &gt; div')
</code></pre>

python html beautifulsoup