Reputation: 1672
I have html structured like this:
<div class="value_i_need_to_match">
<div>
<a href="..."</a>
<a href="..."</a>
<a href="..."</a>
</div>
</div>
<div class="some_other_value">
<div>
<a href="..."</a>
<a href="..."</a>
<a href="..."</a>
</div>
</div>
I need to extract all <a>
elements second level parent of which has class attribute with value that matches to value_i_need_to_match. How to do this?
I tried:
soup_post.find_all(
lambda tag: tag.name == "a" and tag.parent.parent.find('div').attrs['class'] is 'value_i_need_to_match'))
and
soup_post.find_all(
lambda tag: tag.name == "a" and tag.findParent('div').attrs["class"] == "value_i_need_to_match"))
Upvotes: 1
Views: 45
Reputation: 473893
We can do that in a single go with a CSS selector:
soup_post.select(".value_i_need_to_match > div > a")
where >
means a direct parent-child relationship.
Upvotes: 2