问题
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"?
回答1:
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']]
来源:https://stackoverflow.com/questions/10777250/beautifulsoup-findall-not-within-certain-tag