How can I find one tag between two other tags?
<p>I have a document with structure like this:</p>

<pre><code>&lt;tag1&gt;some_text_1&lt;/tag1&gt;
&lt;tag2&gt;text_1&lt;/tag2&gt;
&lt;tag3&gt;....&lt;/tag3&gt;
&lt;tag2&gt;text_2&lt;/tag2&gt;
&lt;tag1&gt;some_text_2&lt;/tag1&gt;
&lt;tag2&gt;text_3&lt;/tag2&gt;
...
</code></pre>

<p>And I need to get all <code>tag2</code> instances that are after <code>tag1</code> with <code>some_text_1</code> and before the next <code>tag1</code>.</p>

<pre><code>from bs4 import BeautifulSoup 

html = '''&lt;tag1&gt;some_text_1&lt;/tag1&gt;
        &lt;tag2&gt;text_1&lt;/tag2&gt;
    &lt;tag3&gt;....&lt;/tag3&gt;
    &lt;tag2&gt;text_2&lt;/tag2&gt;
    &lt;tag1&gt;some_text_2&lt;/tag1&gt;
    &lt;tag2&gt;text_3&lt;/tag2&gt;'''

soup = BeautifulSoup(html,"html.parser")

def findalltags(tag1,tag2,soup):
    # tag1 is between which tag
    # tag2 get info of which tag
    a = soup.find(tag1)
    lis = []
    while True:
        a = a.find_next()
        if(str(a.name) == tag1):
            break
        elif(str(a.name) == tag2):
            lis.append(a)
    return lis
if __name__ == '__main__':
    print findalltags('tag1','tag2',soup)
</code></pre>

<p>Hope this will solve the problem but I don't think this is an efficient way. You can use regular expressions if you familiar with them.</p>

python python-3.x beautifulsoup