Using BeautifulSoup, is it possible to move to the parent tag when using the search for text function?
<p>Is it possible to move from the current position in the DOM up and down when only the text is an common identifier?</p>

<pre><code>&lt;div&gt;changing text&lt;/div&gt;
    &lt;div&gt;fixed text&lt;/div&gt;
</code></pre>

<p>How to get the text <code>changing text</code> when searching for the <code>fixed text</code> and moving up to parent div? </p>

<p>What I tried:</p>

<pre><code>x = soup.body.findAll(text=re.compile('fixed text')).parent

AttributeError: 'ResultSet' object has no attribute 'parent'
</code></pre>

<p>The error you are having is due to call <code>parent</code> in a ResultSet, a list of results. If you need to have multiple results, try:</p>

<pre><code>x = soup.body.find_all(text=re.compile('fixed text'))
for i in x:
    previous_div = i.previous_sibling
</code></pre>

<p>If you doesnt want to find multiple results, just change find_all to find: </p>

<pre><code>x = soup.body.find(text=re.compile('fixed text')).previous_sibling
</code></pre>

<p>Note that I replace parent to previous_sibling, as the divs are in the same level</p>

python beautifulsoup