BeautifulSoup - findAll not within certain tag

若如初见. 提交于 2019-12-23 08:52:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!