2010-03-24 17 views
5

मैं इस तरह के एक दस्तावेज मिल गया है के बीच भाई नोड्स को निकालने के लिए:BeautifulSoup उपयोग दो नोड्स

<p class="top">I don't want this</p> 

<p>I want this</p> 
<table> 
    <!-- ... --> 
</table> 

<img ... /> 

<p> and all that stuff too</p> 

<p class="end>But not this and nothing after it</p> 

मैं पी [वर्ग = शीर्ष] और पी [वर्ग = अंत] पैराग्राफ के बीच सब कुछ निकालना चाहते हैं।

क्या सुंदरसप के साथ मैं ऐसा कर सकता हूं?

from BeautifulSoup import BeautifulSoup 

soup = BeautifulSoup(html) 

nextNode = soup.find('p', {'class': 'top'}) 
while True: 
    # process 
    nextNode = nextNode.nextSibling 
    if getattr(nextNode, 'name', None) == 'p' and nextNode.get('class', None) == 'end': 
     break 

यह जटिल शर्त यह है कि आप HTML टैग और नहीं स्ट्रिंग नोड्स की विशेषताओं तक पहुँच रहे हैं सुनिश्चित करने के लिए है:

उत्तर

8

node.nextSibling विशेषता अपने समाधान है।

संबंधित मुद्दे