title: Efficient way to parse html when only need first occurrence of specific tag
body: <p>I have a large html document. I am parsing it using BeautifulSoup, but the only information I wish to extract is the text of a specific tag (which I believe only occurs once).</p>
  
  <p>Document is formatted like:</p>
  
  <pre><code>    &lt;html&gt;
          &lt;div class=...&gt;
              &lt;div class=...
                  &lt;div class="foo"&gt;
      ...
      ...
  </code></pre>
  
  <p>Currently, I am using a SoupStrainer to filter for only div tags with the class "foo".
  Perhaps I should be using a different library, or perhaps I misunderstand what SoupStrainer is doing. </p>
  
  <pre class="lang-py prettyprint-override"><code>    html = open("file.html", encoding="UTF8").read()
      parse_only = SoupStrainer("div", class_="foo")
      soup = BeautifulSoup(raw_html, "lxml", parse_only=parse_only)
      text = soup.text
  </code></pre>
  
  <p>I hoped this would be significantly faster than it was without the SoupStrainer, but I only got about a 2x speed-up and I would like it to be faster. I think that the reason for this is that it still has to check whether every single tag in the document matches  when straining it. I would like it to simply stop at the first tag that matches that and not spend time parsing any more of the document.</p>