如何用BeautifulSoup选择html的特定部分?

时间:2015-07-03 15:45:57

标签: python beautifulsoup

下面的html是从Yahoo Finance解析的:

<TABLE class="yfnc_tabledata1" width="100%" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD>
      <TABLE width="100%" cellpadding="2" cellspacing="0" border="0">
        <TR class="yfnc_modtitle1" style="border-top:none;">
          <td colspan="2" style="border-top:2px solid #000;">
            <small>
              <span class="yfi-module-title">Period Ending</span>
            </small>
          </td>
          <th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2014</th>
          <th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2013</th>
          <th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2012</th>
        </TR>
        <tr>
          <td colspan="2">
            <strong>
              Total Revenue
            </strong>
          </td>
          <td align="right">
            <strong>
              4,479,648&nbsp;&nbsp;
            </strong>
          </td>
          <td align="right">
            <strong>
              3,777,068&nbsp;&nbsp;
            </strong>
          </td>
          <td align="right">
            <strong>
              3,209,782&nbsp;&nbsp;
            </strong>
          </td>
        </tr>
        <tr>
          <td colspan="2">Cost of Revenue</td>
          <td align="right">3,160,470&nbsp;&nbsp;</td>
          <td align="right">2,656,189&nbsp;&nbsp;</td>
          <td align="right">2,284,485&nbsp;&nbsp;</td>
        </tr>
      </TABLE>
    </TD>
  </TR>
</TABLE>

我想选择此部分下面的所有html

<TABLE class="yfnc_tabledata1" width="100%" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD>
      <TABLE width="100%" cellpadding="2" cellspacing="0" border="0">
        <TR class="yfnc_modtitle1" style="border-top:none;">
          <td colspan="2" style="border-top:2px solid #000;">
            <small>
              <span class="yfi-module-title">Period Ending</span>
            </small>
          </td>
          <th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2014</th>
          <th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2013</th>
          <th scope="col" style="border-top:2px solid #000;text-align:right; font-weight:bold">Dec 31, 2012</th>
        </TR>

如何使用BeautifulSoup:select()方法实现此目的?或任何其他替代方式?

PS: 如果你愿意,请指教我。

0 个答案:

没有答案