我有一个结构如下的页面:
<div id ="a">
<table>
<td>
<!-- many tables and divs here -->
</td>
<td>
<table></table>
<table></table>
<div class="tabber">
<table></table>
<table></table> <!-- TARGET TABLE -->
</div>
</td>
</table>
</div>
没错,不幸的是,除了“tabber”之外,目标上或它附近没有id或类。在
我试着得到那个div元素:
^{pr2}$但它不起作用,总是什么也不输出。在
我试着从一开始就遍历整棵树以得到目标表:
stats_table = soup.find(id='a').findChildren('table')[0].findChildren('td')[1].findChildren('div')[0].findChildren('table')[1]
但也不管用。显然,findChildren('td')
没有得到第一个表的直接子表,而是得到所有子表。超过100个td元件。在
我怎么得到一个直接的元素?在
有没有一种更干净的方法来遍历这么难看的嵌套树?
为什么我不能按类选择div
?它会简化一切。在
您显示的代码似乎没有反映该页上的任何内容:
id='a'
的div
标记。事实上,没有一个标记具有该属性。这就是上一个命令stats_table = ...
失败的原因。在正好有3个
div
标记的class属性等于tabber
,而不是4:它们也不是空的:
类
tabber
中没有一个div
标记,它只有2个table
子类,但我认为这是因为您大大减少了自己的示例。如果你想抓取这样一个网站,你不能很容易地用一个唯一的
id
来选择标签,那么你别无选择,只能帮助自己使用其他属性,比如标签名。有时标记在DOM中的位置相互比较也是一种有用的技术。在对于您的特定问题,您可以使用
title
属性来达到最佳效果:最后一部分是有趣的部分:右下角的所有表都有
title
属性,这将使您能够更容易地选择它们。此外,这些属性使标记在soup中是唯一的,因此您可以直接从根中选择它们:这3项分别对应于“当前连胜”、“得分”和“主客场优势”子项。在
相关问题 更多 >
编程相关推荐