<p>此示例仅从表中选择非“colspan”行:</p>
<pre><code>from bs4 import BeautifulSoup
html_doc = """<tbody>
<tr>
<td colspan="100%" class="info_row">Disks</td>
</tr>
<tr>
<td> sda </td>
<td> 123456 </td>
<td> abcdefg </td>
</tr>
<tr>
<td> sdb </td>
<td> 123456 </td>
<td> abcdefg </td>
</tr>
</tbody>"""
soup = BeautifulSoup(html_doc, "html.parser")
for tr in soup.select("tr:not(:has([colspan]))"):
print(*[td.get_text(strip=True) for td in tr.select("td")])
</code></pre>
<p>印刷品:</p>
<pre><code>sda 123456 abcdefg
sdb 123456 abcdefg
</code></pre>
<hr/>
<p>或者:您可以选择包含单元格“磁盘”的行之后的所有行:</p>
<pre><code>for tr in soup.select('tr:has(> td:contains("Disks")) ~ tr'):
print(*[td.get_text(strip=True) for td in tr.select("td")])
</code></pre>
<hr/>
<p>编辑:使用<code>lambda</code>:</p>
<pre><code>for tr in soup.find(lambda tag: tag.name == "tr" and tag.text.strip() == "Disks").find_next_siblings("tr"):
print(*[td.get_text(strip=True) for td in tr.select("td")])
</code></pre>