[...]
soup = BeautifulSoup(url)
for row in soup.find("table", { "class" : "grid-border-args" }).findAll("tr"):
if row.find("td", {"class" : "row-label-one"}) != None:
uur = row.find("td", {"class" : "row-label-one"})
if row.find("td", {"class" : "object-cell-border"}) != None:
vakken = row.findAll("td", {"class" : "object-cell-border"})
for vak in vakken:
duur = vak.find("td", {"class" : "object-cell-border"}).attrs["rowspan"]
此代码呈现错误:“'NoneType'对象没有属性'attrs'” 返回“none”确实是正确的,但我不明白为什么,因为每个'vak'都以td类开头,并且具有我正在寻找的属性。例如:
<td class="object-cell-border" colspan="1" rowspan="4" style="border-right:3px solid #000000;">
<!-- START OBJECT-CELL -->
<table border="0" cellspacing="0" class="object-cell-args" width="100%">
<col class="object-cell-0-0"></col>
<tr>
<td align="left">Lineaire algebra : stelsels, matrices en afbeeldingen</td>
</tr>
</table>
<table border="0" cellspacing="0" class="object-cell-args" width="100%">
<col class="object-cell-1-0"></col>
<col class="object-cell-1-2"></col>
<tr>
<td align="left">D.0.02</td>
<td align="right">3-6, 8, 10-14</td>
</tr>
</table>
<table border="0" cellspacing="0" class="object-cell-args" width="100%">
<col class="object-cell-2-0"></col>
<col class="object-cell-2-2"></col>
<tr>
<td align="left">CARA PHILIPPE</td>
<td align="right"></td>
</tr>
</table>
<!-- END OBJECT-CELL -->
</td>
为什么不让我获得第一个td-tag?
答案 0 :(得分:2)
这部分需要嵌套td
个元素object-cell-border
:
vakken = row.findAll("td", {"class" : "object-cell-border"})
for vak in vakken:
duur = vak.find("td", {"class" : "object-cell-border"}).attrs["rowspan"]
也就是说,首先您找到该类的所有td
元素并将其分配给vakken
。然后,对于每个td
,您在其中搜索以找到具有相同类的后代td
。