网页抓取:如何测试根标签是否具有特定的 CSS 类?



我有一个汤对象,其中包含以下内容:

<tr class="x--player-is-starter">
<td class="pos" style="display: none; height: 62px;">10</td>
<td class="name" style="display: none; height: 62px;">
<a class="player-profile-link" href="/eurocupwomen/18-19/player/Maria-Conde" target="_blank"> Maria Conde</a>
</td>
<td class="min" style="height: 62px;">29:37</td>
<td class="pts" style="height: 62px;">13</td>
<td class="field-goals" style="height: 62px;">
<span class="made-all">4/8</span>
<span class="percent">50%</span>
</td>
<td class="field-goals-2p" style="height: 62px;">
<span class="made-all">1/2</span>
<span class="percent">50%</span>
</td>
<td class="field-goals-3p" style="height: 62px;">
<span class="made-all">3/6</span>
<span class="percent">50%</span>
</td>
<td class="free-throw" style="height: 62px;">
<span class="made-all">2/4</span>
<span class="percent">50%</span>
</td>
<td class="reb-offence" style="height: 62px;">2</td>
<td class="reb-defence" style="height: 62px;">0</td>
<td class="reb-total" style="height: 62px;">2</td>
<td class="assists" style="height: 62px;">3</td>
<td class="personal-fouls" style="height: 62px;">0</td>
<td class="turnovers" style="height: 62px;">1</td>
<td class="steals" style="height: 62px;">3</td>
<td class="block-shots" style="height: 62px;">0</td>
<td class="plus-minus" style="height: 62px;">2</td>
<td class="efficiency" style="height: 62px;">14</td>
</tr>

我想知道如何知道标签"tr"是否具有 CSSclass = "x--player-is-starter"

如果这个包含上述tr的对象被称为row,例如,我尝试使用row.find("tr", class_="x--player-is-starter"),但结果总是得到"无"。

那么,我如何知道"tr"标签是否具有我尝试查找的 CSS 类?我做错了什么吗?

编辑 I:

我获取"tr"标签内的内容没有问题,但我想知道该标签"tr"是否具有 CSSclass = "x--player-is-starter"

如果可能的话,我想得到一个TrueFalse,例如:

<tr class = "x--player-is-starter">返回True<tr class = "">返回False

我该怎么做?

我会保持更通用并使用css标签选择器然后测试每个类

soup = BeautifulSoup(html, 'lxml')
results = [(i.get('class'), True) if i.get('class')[0] == "x--player-is-starter" else (i.get('class'), False) for i in soup.select('tr')]
print(results)

最新更新