find the CSS path (ancestor tags) in HTML using python
<p>I want to get all the ancestor div tags where I match a text. So for example if the html looks like <a href="http://i.stack.imgur.com/aePNZ.png" rel="nofollow">HTML snippet</a></p>

<p>And i'm searching for "Earl E. Byrd". I wanna get a list which contains {"buyer-info","buyer-name"}</p>

<p>This is what i did </p>

<pre><code>r=requests.get(self.url,verify='/path/to/certfile')
                soup = BeautifulSoup(r.text,"lxml")
                divTags = soup.find_all('div')
</code></pre>

<p>How should I proceed ?</p>

<p>If you want to search for the div by text and get all the previous divs that have <em>title</em> attributes, first find the div using the text, then use  <code>find_all_previous</code> setting <code>title=True</code></p>

<pre><code>soup = BeautifulSoup(r.text,"lxml")
div = soup.find('div', text="Earl E. Byrd")

print([div["title"]] + [d["title"] for d in div.find_all_previous("div", title=True)])
</code></pre>

python web-scraping bs4