【问题标题】:How to get a text depending on another text's tag如何根据另一个文本的标签获取文本
【发布时间】:2020-07-18 16:48:27
【问题描述】:

由于我要提取的文本都具有相同的标签,即 (nobr),因此我无法找到一种方法来根据文本本身获取 nobr 标签中的特定文本。

更准确地说,这里是我正在研究的 html 汤的一个例子:

我想在一种字典中提取一个键值对,其中一个键表示文章的编号(在示例中为 2 星之间)和包含所有文本/cmets 的值(在 3 星之间)例子)。在此示例中,仅表示 2 篇文章,但原始 html 包含数百篇文章。 cmets 是不同连续 nobr 标记中的分隔短语。

感谢您的帮助。

        <font size="+2">
         <b>
         </b>
        </font>    </p>    <p>
        <font style="font-family:monospaced">
         <table border="1" bordercolor="black" cellpadding="1" cellspacing="0" class="list" rules="groups">
          <colgroup>
          </colgroup>
          <tbody>
           <tr>
            <td style="background:#E8EAD8">
             <font color="#0273bc" face="courier new" size="2">
              <nobr id="l0002003">
               Article          :    **00154659**                          
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#E8EAD8">
             <font face="courier new" size="2">
              <nobr id="l0003003">
               Description      :    jkqlqd-ddod-scncn
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#E8EAD8">
             <font color="#0273bc" face="courier new" size="2">
              <nobr id="l0004003">
               N° de plan       :    86265487
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#E8EAD8">
             <font face="courier new" size="2">
              <nobr id="l0005003">
               N°mod.doc.       :    I
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#E8EAD8">
             <font color="#0273bc" face="courier new" size="2">
              <nobr id="l0006003">
               Cond. De temp.   :
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#E8EAD8">
             <font face="courier new" size="2">
              <nobr id="l0007003">
               Classe Matière   :    abc_hjg
              </nobr>
             </font>
            </td>
           </tr>
          </tbody>
          <tbody>
          </tbody>
         </table>
         <table border="1" bordercolor="black" cellpadding="1" cellspacing="0" class="list" rules="groups">
          <colgroup>
          </colgroup>
          <tbody>
           <tr>
            <td style="background:#cef8ae">
             <font face="courier new" size="2">
              <nobr id="l0010003">
               Notice        français
              </nobr>
             </font>
             <font face="courier new" size="2">
              <nobr id="l0010046" style="cursor:hand;background:#cef8ae">
               00154659
              </nobr>
             </font>
             <font face="courier new" size="2">
              <nobr id="l0010054" style="background:#cef8ae">
              </nobr>
             </font>
            </td>
           </tr>
          </tbody>
          <tbody>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0012003">
               ***Before doing this get that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0013003">
               and that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0014003">
               or that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0015003">
               and also that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0016003">
               this that 
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0017003">
               __________________________________
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0018003">
               also 1
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0019003">
               and 2
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0020003">
               and 3
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0021003">
               and 4
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0022003">
               and 5
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0023003">
               __________________________________***
              </nobr>
             </font>
            </td>
           </tr>
          </tbody>
          <tbody>
          </tbody>
         </table>
         <font face="courier new" size="2">
          <span style="white-space:nowrap">
           <font face="courier new" size="2">
            <nobr id="l0025002" style="background:#E8EAD8">
            </nobr>
           </font>
          </span>
         </font>
         <br/>
         <table border="1" bordercolor="black" cellpadding="1" cellspacing="0" class="list" rules="groups">
          <colgroup>
          </colgroup>
          <tbody>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0027003">
               Article          :    **00157846**
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0028003">
               Description      :    jdkr-lki-lme
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0029003">
               N° de plan       :    8626547
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0030003">
               N°mod.doc.       :    I
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0031003">
               Cond. De temp.   :
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0032003">
               Classe Matière   :    abc_hjg
              </nobr>
             </font>
            </td>
           </tr>
          </tbody>
          <tbody>
          </tbody>
         </table>
         <table border="1" bordercolor="black" cellpadding="1" cellspacing="0" class="list" rules="groups">
          <colgroup>
          </colgroup>
          <tbody>
           <tr>
            <td style="background:#cef8ae">
             <font face="courier new" size="2">
              <nobr id="l0035003">
               Notice        français
              </nobr>
             </font>
             <font face="courier new" size="2">
              <nobr id="l0035046" style="cursor:hand;background:#cef8ae">
               00157846
              </nobr>
             </font>
             <font face="courier new" size="2">
              <nobr id="l0035054" style="background:#cef8ae">
              </nobr>
             </font>
            </td>
           </tr>
          </tbody>
          <tbody>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0037003">
               ***Before doing this as well
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0038003">
               verify this and that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0039003">
               in the same this and that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0040003">
               and also that
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0041003">
               try also this
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0042003">
               __________________________________
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0043003">
               also 1
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0044003">
               and 2
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0045003">
               and 3
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0046003">
               and 4
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#eef9ff">
             <font face="courier new" size="2">
              <nobr id="l0047003">
               and 5
              </nobr>
             </font>
            </td>
           </tr>
           <tr>
            <td style="background:#d7ecf4">
             <font face="courier new" size="2">
              <nobr id="l0048003">
               __________________________________***
              </nobr>
             </font>
            </td>
           </tr>
          </tbody>
          <tbody>
          </tbody>
         </table>
         <font face="courier new" size="2">
          <span style="white-space:nowrap">
           <font face="courier new" size="2">
            <nobr id="l0050002" style="background:#E8EAD8">
            </nobr>
           </font>
          </span>
         </font>
         <br/>
         <font face="courier new" size="2">
          <nobr>
           <strike>
           </strike>
          </nobr>
         </font>
         <br/>
         <font face="courier new" size="2">
          <span style="white-space:nowrap">
           <font face="courier new" size="2">
            <nobr id="l0052002" style="background:#fefeb8">
             Nombre de données          2
            </nobr>
           </font>
          </span>
         </font>
         <br/>
         <font face="courier new" size="2">
          <nobr>
           <strike>
           </strike>
          </nobr>
         </font>
         <br/>
         <font face="courier new" size="2">
          <span style="white-space:nowrap">
           <font face="courier new" size="2">
            <nobr id="l0054002" style="background:#cef8ae">
             Début      18:56:29
            </nobr>
           </font>
          </span>
         </font>
         <br/>
         <font face="courier new" size="2">
          <span style="white-space:nowrap">
           <font face="courier new" size="2">
            <nobr id="l0055002" style="background:#cef8ae">
             Fin        18:56:31
            </nobr>
           </font>
          </span>
         </font>
         <br/>
         <font face="courier new" size="2">
          <span style="white-space:nowrap">
           <font face="courier new" size="2">
            <nobr id="l0056002" style="background:#cef8ae">
             Durée      00:00:02
            </nobr>
           </font>
          </span>
         </font>
         <br/>
        </font>    </p>   </blockquote>  </body> </html> ```

【问题讨论】:

  • 欢迎来到 Stack Overflow!这不是一个人们为您编写代码以便您不必编写代码的网站。如果您需要帮助调试您编写的代码,您必须发布Minimal, Complete, and Verifiable example 并解释您的代码的具体问题。

标签: python html beautifulsoup


【解决方案1】:

如果txt 包含问题中的 HTML 代码,则此脚本:

from pprint import pprint
from bs4 import BeautifulSoup


soup = BeautifulSoup(txt, 'html.parser')

all_data = {}
for table in soup.select('table:has(nobr:contains("Article"))'):
    article_nr = table.select_one('nobr:contains("Article")').get_text(strip=True).split()[-1]
    text = table.find_next('table').select('tbody')[1].get_text(strip=True, separator='\n')
    all_data[article_nr] = text

pprint(all_data)

产生这个字典:

{'**00154659**': '***Before doing this get that\n'
                 'and that\n'
                 'or that\n'
                 'and also that\n'
                 'this that\n'
                 '__________________________________\n'
                 'also 1\n'
                 'and 2\n'
                 'and 3\n'
                 'and 4\n'
                 'and 5\n'
                 '__________________________________***',
 '**00157846**': '***Before doing this as well\n'
                 'verify this and that\n'
                 'in the same this and that\n'
                 'and also that\n'
                 'try also this\n'
                 '__________________________________\n'
                 'also 1\n'
                 'and 2\n'
                 'and 3\n'
                 'and 4\n'
                 'and 5\n'
                 '__________________________________***'}

【讨论】:

  • 谢谢安德烈!
猜你喜欢
  • 2011-10-07
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多