Remove html after some point in Beautilful Soup
<p>I have a trouble. My aim is to parse the data until some moment. Then, I want to stop parsing.</p>

<pre><code>        &lt;span itemprop="address"&gt;
         Some address
        &lt;/span&gt;
        &lt;i class="fa fa-signal"&gt;
        &lt;/i&gt;
        ...
       &lt;/p&gt;
      &lt;/div&gt;
     &lt;/div&gt;
     &lt;div class="search_pagination" id="pagination"&gt;
      &lt;ul class="pagination"&gt;
      &lt;/ul&gt;
     &lt;/div&gt;
    &lt;/div&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="col-sm-3"&gt;
   &lt;div class="panel" itemscope="" itemtype="http://schema.org/WPSideBar"&gt;
    &lt;h2 class="heading_a" itemprop="name"&gt;
     Top-10 today
    &lt;/h2&gt; #a lot of tags after that moment
</code></pre>

<p>I want to get all the values from <code>&lt;span itemprop="address"&gt;</code> (there are a lot of them before) until the moment <code>Top-10 today</code>.</p>

<p>You can actually let <code>BeautifulSoup</code> <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#parsing-only-part-of-a-document" rel="nofollow">parse only the tags you are interested in via <code>SoupStrainer</code></a>:</p>

<pre><code>from bs4 import BeautifulSoup, SoupStrainer

only_addresses = SoupStrainer("span", itemprop="address")
soup = BeautifulSoup(html_doc, "html.parser", parse_only=only_addresses)
</code></pre>

<p>If you though have some "addresses" before the "Top-10 today" and some after but you are interested in those coming before it, you can make a custom <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-function" rel="nofollow">searching function</a>:</p>

<pre><code>def search_addresses(tag):
    return tag.name == "span" and tag.get("itemprop") == "address" and \
           tag.find_next("h2", text=lambda text: text and "Top-10 today" in text)

addresses = soup.find_all(search_addresses)
</code></pre>

<p>It does not look trivial, but the idea is simple - we are using <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all-next-and-find-next" rel="nofollow"><code>find_next()</code></a> for every "address" to check if "Top-10 today" heading exists after it.</p>

python html beautifulsoup