【问题标题】:Return an uncertain number of paragraphs in BeautifulSoup在 BeautifulSoup 中返回不确定的段落数
【发布时间】:2012-11-16 14:44:51
【问题描述】:

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

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

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    您需要遍历部分,然后遍历段落。出于演示目的,我已修改您的代码以打印每个段落的文本。

    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
    

    【讨论】:

    • 这些段落实际上并不在这些部分中。该部分就在段落之前,它们唯一的其他标识符是一个巨大的 div,这些段落包含许多不太相关的段落。
    • &lt;section&gt;&lt;p&gt; paragraph text &lt;/p&gt;&lt;/section&gt; “元素”部分实际上是开始标签和结束标签之间的所有内容,而不仅仅是开始标签本身。因此,这些段落实际上是在 inside 部分。
    • nonono,它是

      这就是为什么我不得不使用兄弟。
    • @SlaterTyranus 我提供的代码肯定会返回您提供的链接上的所有文本,您是否真的尝试过运行它?
    • 是的,事实上我有,但它什么也没有返回......有没有可能这是因为不同的 Beautiful Soup 版本?
    猜你喜欢
    • 2016-11-20
    • 2021-01-18
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多