Using python3, what is the fastest way to extract all <div> blocks from a html str?
<p>there are <code>&lt;div&gt;</code> inner blocks inside a <code>&lt;div&gt;</code> block,
What is the fastest way to extract all <code>&lt;div&gt;</code> blocks from a html str ?
(bs4, lxml or regex ?)</p>

<p>When I'm teaching XML/HTML parsing with Python, I use to show this levels of complexity:</p>

<ol>
<li>RegEx: efficient for (very) simple parsing but can be/become hard to maintain.</li>
<li>SAX: efficient and safe to parse XML as a stream. Easy to extract pieces of data but awful when you want to transform the tree. Can become really difficult to maintain. Who still use that anyway?</li>
<li>DOM parsing or ElementTree parsing with <strong>lxml</strong>: less efficient: all the XML tree is loaded in memory (can be an issue for big XML). But this library is compiled (in Cython). Very popular and reliable. Easy to understand: the code can be maintained.</li>
<li>XSLT1 is also a possibility. Very good to transform the tree in depth. But not efficient because of the templates machinery. Need learn a new language which appears to be difficult to learn. Maintenance often become heavy. Note that <strong>lxml</strong> can do XSLT with Pyton functions as an extension. </li>
<li>XSLT2 is very powerful but the only implementation I know is in Java language with Saxon. Launching the JRE is time consuming. The language is difficult to learn. One need to be an expert to understand every subtleties. Worse as XSLT1.</li>
</ol>

<p>For your problem, <strong>lxml</strong> (or <strong>BeautifulSoup</strong>) sound good.</p>

python html regex parsing