1

我刚开始使用 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>

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

4

1 回答 1

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
于 2012-11-28T17:32:45.947 回答