Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup - findAll not within certain tag

So I'm trying to find a way to find all items within a BeautifulSoup object that have a certain tag that aren't within a certain other tag. For example:

<td class="disabled first"> <div class="dayContainer">
      <p class="day"> 29
      </p> <p class="moreLink">
      </p> 
   </div>
</td> 

I want to find all iterations of class="dayContainer", which is simple enough, but how do I go about finding all of those that aren't first within class="diabled"?

like image 356
westbyb Avatar asked May 27 '12 21:05

westbyb


1 Answers

Run a filter for tags whose .parent doesn't have that class attribute. Something like

filteredDayContainers = [tag for tag in soup.find_all('div', 
    attrs = {'class': 'dayContainer'}) 
    if "disabled" not in tag.parent['class']]
like image 138
Ansari Avatar answered Nov 11 '22 23:11

Ansari