返回BeautifulSoup中不确定数量的段落

时间:2012-11-28 16:55:04

标签: python html web-scraping 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>

想要了解这些段落中的内容。

1 个答案:

答案 0 :(得分: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