我是蟒蛇美湯的新手,很長一段時間以來我一直試圖為我的問題找到答案。
我嘗試從網站上抓取資料,它有很多表格和 td。對我來說,有 2 個屬性相同但資料使用不同的 td。當我得到所有這些 td 時,我如何區分它們?目標是將它們存盤在不同的串列中。
HTML 如下所示:
<td class = "xyz">
<h4 class = "zyw">
" 1"
<small class = "unit">" m" </small>
</h4>
</td>
<td class = "xyz">
<h4 class = "zyw">
" 8"
<small class = "unit">" s" </small>
</h4>
</td>
我設法使用以下代碼獲取兩者的資料:
for td_swellHeight in tr.find_all('td', {'class':'text-center background-gray-lighter'}):
for h4_swellHeight in td_swellHeight.find_all('h4'):
print(h4_swellHeight.text)
輸出如下所示:

謝謝!
uj5u.com熱心網友回復:
您可以使用索引跳過每秒 td:
for i,td_swellHeight in enumerate(tr.find_all('td', {'class':'text-center background-gray-lighter'})):
if i % 2 == 0:
continue
for h4_swellHeight in td_swellHeight.find_all('h4'):
print(h4_swellHeight.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/412065.html
標籤:
