<i id='4zWF1'><tr id='4zWF1'><dt id='4zWF1'><q id='4zWF1'><span id='4zWF1'><b id='4zWF1'><form id='4zWF1'><ins id='4zWF1'></ins><ul id='4zWF1'></ul><sub id='4zWF1'></sub></form><legend id='4zWF1'></legend><bdo id='4zWF1'><pre id='4zWF1'><center id='4zWF1'></center></pre></bdo></b><th id='4zWF1'></th></span></q></dt></tr></i><div id='4zWF1'><tfoot id='4zWF1'></tfoot><dl id='4zWF1'><fieldset id='4zWF1'></fieldset></dl></div>
    1. <tfoot id='4zWF1'></tfoot><legend id='4zWF1'><style id='4zWF1'><dir id='4zWF1'><q id='4zWF1'></q></dir></style></legend>

      <small id='4zWF1'></small><noframes id='4zWF1'>

        <bdo id='4zWF1'></bdo><ul id='4zWF1'></ul>

        使用 beautifulsoup 查找下一个兄弟姐妹,直到某个兄弟姐妹

        Find next siblings until a certain one using beautifulsoup(使用 beautifulsoup 查找下一个兄弟姐妹,直到某个兄弟姐妹)

          <tfoot id='BxCfH'></tfoot>

            <bdo id='BxCfH'></bdo><ul id='BxCfH'></ul>
            <legend id='BxCfH'><style id='BxCfH'><dir id='BxCfH'><q id='BxCfH'></q></dir></style></legend>
            <i id='BxCfH'><tr id='BxCfH'><dt id='BxCfH'><q id='BxCfH'><span id='BxCfH'><b id='BxCfH'><form id='BxCfH'><ins id='BxCfH'></ins><ul id='BxCfH'></ul><sub id='BxCfH'></sub></form><legend id='BxCfH'></legend><bdo id='BxCfH'><pre id='BxCfH'><center id='BxCfH'></center></pre></bdo></b><th id='BxCfH'></th></span></q></dt></tr></i><div id='BxCfH'><tfoot id='BxCfH'></tfoot><dl id='BxCfH'><fieldset id='BxCfH'></fieldset></dl></div>
              <tbody id='BxCfH'></tbody>
            • <small id='BxCfH'></small><noframes id='BxCfH'>

                • 本文介绍了使用 beautifulsoup 查找下一个兄弟姐妹,直到某个兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  网页是这样的:

                  <h2>section1</h2><p>文章</p><p>文章</p><p>文章</p><h2>section2</h2><p>文章</p><p>文章</p><p>文章</p>

                  我怎样才能找到其中包含文章的每个部分?即找到h2后,找到nextsiblings

                  直到下一个h2.

                  如果网页是这样的:(通常是这种情况)

                  <h2>部分 1</h2><p>文章</p><p>文章</p><p>文章</p></div>

                  <h2>section2</h2><p>文章</p><p>文章</p><p>文章</p></div>

                  我可以写如下代码:

                  soup.findAll('div') 中的部分:...对于 section.findAll('p') 中的帖子

                  但是如果我想获得相同的结果,我应该如何处理第一个网页呢?

                  解决方案

                  我认为你可以这样做:

                  soup.findAll('h2') 中的部分:下一个节点 = 部分而真:nextNode = nextNode.nextSibling尝试:tag_name = nextNode.name除了属性错误:标签名 = ""如果 tag_name == "p":打印 nextNode.string别的:打印 "*****"休息

                  给定:

                  <h2>section1</h2><p>文章 1</p><p>文章 2</p><p>文章 3</p><h2>section2</h2><p>文章 4</p><p>第 5 条</p><p>第 6 条</p>

                  输出:

                  文章1第2条第3条*****第4条第五条第六条*****

                  The webpage is something like this:

                  <h2>section1</h2>
                  <p>article</p>
                  <p>article</p>
                  <p>article</p>
                  
                  <h2>section2</h2>
                  <p>article</p>
                  <p>article</p>
                  <p>article</p>
                  

                  How can I find each section with articles within them? That is, after finding h2, find nextsiblings

                  until the next h2.

                  If the webpage were like: (which is normally the case)

                  <div>
                  <h2>section1</h2>
                  <p>article</p>
                  <p>article</p>
                  <p>article</p>
                  </div>
                  
                  <div>
                  <h2>section2</h2>
                  <p>article</p>
                  <p>article</p>
                  <p>article</p>
                  </div>
                  

                  I can write codes like:

                  for section in soup.findAll('div'):
                  ...
                      for post in section.findAll('p')
                  

                  But what should I do with the first webpage if I want to get the same result?

                  解决方案

                  I think you can do something like this:

                  for section in soup.findAll('h2'):
                      nextNode = section
                      while True:
                          nextNode = nextNode.nextSibling
                          try:
                              tag_name = nextNode.name
                          except AttributeError:
                              tag_name = ""
                          if tag_name == "p":
                              print nextNode.string
                          else:
                              print "*****"
                              break
                  

                  Given:

                  <h2>section1</h2>
                  <p>article1</p>
                  <p>article2</p>
                  <p>article3</p>
                  
                  <h2>section2</h2>
                  <p>article4</p>
                  <p>article5</p>
                  <p>article6</p>
                  

                  Output:

                  article1
                  article2
                  article3
                  *****
                  article4
                  article5
                  article6
                  *****
                  

                  这篇关于使用 beautifulsoup 查找下一个兄弟姐妹,直到某个兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                        <tbody id='eIKr1'></tbody>
                      <tfoot id='eIKr1'></tfoot>
                      <i id='eIKr1'><tr id='eIKr1'><dt id='eIKr1'><q id='eIKr1'><span id='eIKr1'><b id='eIKr1'><form id='eIKr1'><ins id='eIKr1'></ins><ul id='eIKr1'></ul><sub id='eIKr1'></sub></form><legend id='eIKr1'></legend><bdo id='eIKr1'><pre id='eIKr1'><center id='eIKr1'></center></pre></bdo></b><th id='eIKr1'></th></span></q></dt></tr></i><div id='eIKr1'><tfoot id='eIKr1'></tfoot><dl id='eIKr1'><fieldset id='eIKr1'></fieldset></dl></div>

                      <small id='eIKr1'></small><noframes id='eIKr1'>

                        <legend id='eIKr1'><style id='eIKr1'><dir id='eIKr1'><q id='eIKr1'></q></dir></style></legend>
                        • <bdo id='eIKr1'></bdo><ul id='eIKr1'></ul>