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

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

      <bdo id='FddHr'></bdo><ul id='FddHr'></ul>

      <tfoot id='FddHr'></tfoot>
      1. 如何从BeautifulSoup表的TD中获取价值?

        时间:2024-04-20

            <tbody id='zc84I'></tbody>

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

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

                  本文介绍了如何从BeautifulSoup表的TD中获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个页面,其源代码中包含一些表:

                  <table width='100%' cellspacing='0' cellpadding='2' class='an'>
                      <tr>
                          <td width='35%' align='right'>XXX :</td>
                          <td><b>20</b></td>
                      </tr>
                      <tr><
                          td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                  </table>
                  
                  <table width='361' cellspacing='0' cellpadding='2' class='an'>
                      <tr>
                          <td width='35%' align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XX :</td>
                          <td><a href='XXX'><b>XXX</b></a></td>
                      </tr>
                      <tr>
                          <td align='right'>PHONE :</td>
                          <td><b>518878943</b></td>
                      </tr>
                  </table>
                  

                  我想从此页面获取第二个表中的电话号码:

                  <td align='right'>PHONE :</td>
                  <td><b>518878943</b></td>
                  

                  但是,我的代码:

                  page_src="""<table width='100%' cellspacing='0' cellpadding='2' class='an'>
                      <tr>
                          <td width='35%' align='right'>XXX :</td>
                          <td><b>20</b></td>
                      </tr>
                      <tr><
                          td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                  </table>
                  
                  <table width='361' cellspacing='0' cellpadding='2' class='an'>
                      <tr>
                          <td width='35%' align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XXX :</td>
                          <td><b>XXX</b></td>
                      </tr>
                      <tr>
                          <td align='right'>XX :</td>
                          <td><a href='XXX'><b>XXX</b></a></td>
                      </tr>
                      <tr>
                          <td align='right'>PHONE :</td>
                          <td><b>518878943</b></td>
                      </tr>
                  </table>
                  """
                  
                  soup = BeautifulSoup(page_src, 'html.parser')
                  divs = soup.findAll("table", {"class": "an"})
                  for div in divs:
                      row = ''
                      rows = [row in div.findAll('tbody').findAll('tr')]
                  

                  给我这样一条错误消息:

                  Traceback (most recent call last):
                    File "test.py", line 198, in <module>
                      rows = [row in div.findAll('tbody').findAll('tr')]
                  AttributeError: 'ResultSet' object has no attribute 'findAll'
                  

                  如何解决此问题,并从页面获取电话号码?谢谢

                  编辑:

                  部分解决。部分原因是我认为我的解决方案很难看,但很管用。也许有人会想出更漂亮的解决方案?

                  tds = []
                  soup = BeautifulSoup(page_src, 'html.parser')
                  divs = soup.findAll("table", {"class": "an"})
                  for div in divs:
                      rows = div.findAll('tr')
                      for row in rows :
                          tds.append(row.findAll('td'))
                  phone = str(tds[12][1])
                  phone = phone.replace("<td><b>", "").replace("</b></td>", "").strip()
                  print phone
                  

                  推荐答案

                  找到包含PHONE :td元素,然后获取following sibling element。一行:

                  soup.find("td", text="PHONE :").find_next_sibling("td").text
                  

                  这篇关于如何从BeautifulSoup表的TD中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用美汤从表单中提取隐藏值 下一篇:在给定时间启动功能

                  相关文章

                  1. <small id='9oZuK'></small><noframes id='9oZuK'>

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