2012-11-28 16 views
1

我剛剛開始使用BeautifulSoup,並且正在嘗試製作一個腳本,該腳本將轉到多個非常相似的頁面,然後返回段落下的所有段落。目前,我有以下代碼在BeautifulSoup中返回不確定數量的段落

def BrightstormPageTest(): 
    soup = Soup(urllib.urlopen('http://brightstorm.com/science/chemistry/chemical-reaction-rates/collision-theory/').read()) 
    relevantTagText = "" 
    for element in soup.findAll("section"): 
      print element.nextSibling 

這對於第一款的偉大工程,但有兩個部分,我想兄弟姐妹從和第一部分將始終有一個確切的段落,而第二個將有一個不確定大概在1到10之間。任何想法如何做到這一點?

相關的html:

<section> 
     <div class="page-header"> 
     <h2> 
     Explanation 
     </h2> 
     </div> 
    </section> 
    <p> 
     <strong> 
     Collision theory 
     </strong> 
     is a model for explaining chemical reactions and reaction rates using the interactions of particles within the reactants. There are three important parts to 
     <strong> 
     collision theory 
     </strong> 
     , that reacting substances must collide, that they must collide with enough energy and that they must collide with the correct orientation. Increasing the kinetic energy of these particles or decreasing their volume increases the frequency of collisions and speeds a reaction. 
    </p> 
    <section> 
     <div class="page-header"> 
     <h2> 
     Transcript 
     </h2> 
     </div> 
    </section> 
    <p> 
     Alright so we're going to talk about the collision theory. And the collision theory comes into play when you're talking about reactions and actually what happens in a reaction and how a reaction actually goes from the reactant all the way to the product. So the first thing we're going to have to discuss is, the fact that the reacting substances whatever we're dealing with the atoms, ions or molecules must collide in order for the reaction to occur. Okay that seems pretty obvious so we have our 2 reactants a and b and they must collide, and this is what we're going to call activated complex or a transition states that's going from, transitioning from the reactants towards the product and it's going to recreate this independent, very high energy activated complex and then yield our products, our 2ab. So the first postulate is that they must come together, okay that's easy enough. 
    </p> 
    <p> 
     The second one says the reactant substances must collide with sufficient energy in order to form that activated complex. Because this activated complex is extremely high, very high in energy, very unstable so they must collide with a certain amount of energy to get to this point. If they don't collide with a good amount of energy then they're actually not going to react at all. So that energy is going to be called our activation energy to get to our activated complex. And you might see the symbol e with a subscript a to note that. And the last thing in the collision theory is that reacting substances must collide with the correct orientation so if they, made a collision at a range that wasn't great for them, they would actually rebound off of each other and not react at all. 
    </p> 
    <p> 
     But if they if they did they have to make sure they line up correctly and then for the correct reaction to occur then they get their activated complex to form the products. And so these 3 things are the basis of that collision theory and how reactants go from reactants to the products. 
    </p> 

想只得到這些段落裏面的東西。

回答

0

您需要迭代這些部分,然後迭代段落。爲了演示目的,我修改了代碼以打印每個段落的文本。

from bs4 import BeautifulSoup as Soup 

def BrightstormPageTest(): 
    soup = Soup(urllib.urlopen('http://brightstorm.com/science/chemistry/chemical-reaction-rates/collision-theory/').read()) 
    sections = soup.findAll("section") 
    for section in sections: 
     ps = section.findAll("p") 
     for p in ps: 
      print p.text 

def BrightstormPageTest2(): 
    soup = Soup(urllib.urlopen('http://brightstorm.com/science/chemistry/chemical-reaction-rates/collision-theory/').read()) 
    sections = soup.findAll("section") 
    for section in sections: 
     while True: 
      try: 
       print section.nextSibling.text 
      except TypeError: 
       # .text is a valid method on a <p> element, but not a NavigableString. 
       break 
+0

這些段落實際上並不在段落中。該部分剛剛出現在段落之前,並且它們的唯一其他標識符是段落中包含大量不太相關段落的大量div。 –

+0

'

段落文本

'節'元素'實際上是開始和結束標記之間的所有內容,而不僅僅是開始標記本身。所以,這些段落實際上是在段落內。 – kreativitea

+1

nonono,它是

這就是爲什麼我必須使用兄弟姐妹。 –