问题描述
我正在尝试获取每个 <td>
的第一个 <tr>
标签的值(文本),但没有成功
在下面的例子中,我想要实现的是:juicy vic
如何使用 cheerio
模块获取此值,从 <td>
标签获取这些值的最确定方法是什么?
<table class="partners">
<thead>
<tr>
<th>Nome do Summoner</th>
<th>Posição</th>
<th>Tier</th>
<th>Campeão Recente</th>
<th>Chances</th>
<th>KDA</th>
<th>Nota</th>
<th>RegisTrado</th>
</tr>
</thead>
<tbody id="partners">
<tr class="group" id="partner-86729" phx-click="delete" phx-target="1" data-phx-component="1">
<td>
<a id="partner-86729-name" href="https://br.op.gg/summoner/userName=juicy vic ?utm_source=duo.lol&utm_medium=partner-list&utm_campaign=permanent" target="_blank" phx-hook="StopPropagation" class="partner__profile partner__link">
<img src="//opgg-static.akamaized.net/images/profile_icons/profileIcon3542.jpg?image=q_auto:best&v=1518361200">
juicy vic
</a>
</td>
<td id="partner-86729-edit-position" phx-hook="PartnerEditHook">
<span class="position-container">
<svg class="position-icon support" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M13.991 8.327l2.248-2.036H24c-2.553 2.327-4.69 2.86-5.44 2.836h-1.45l2.03 2.91-3.553 1.527-1.596-5.237zM14.644 19.745L12.758 9.127l-.798.946V22l2.684-2.255zM10.009 8.327L7.76 6.291H0c2.553 2.327 4.69 2.86 5.44 2.836h1.45l-2.03 2.91 3.553 1.527 1.596-5.237zM9.277 19.745l1.886-10.618.797.946V22l-2.683-2.255zM9.048 2L8.25 3.382 11.876 7.6l3.627-4.218L14.56 2H9.048z"></path>
</svg>
<span class="position-container__tooltip">
Suporte
</span>
</span>
</td>
<td>
<div class="flex-container">
<img src="/images/tiers/unranked-73687a6509dfb61e1f84b01150173ed0.png?vsn=d" class="partner__tier">
Unranked
</div>
</td>
<td>
<div class="partner__champions">
</div>
</td>
<td>
0승
0패
<span class="dimmed">
(<span class="red">0%</span>)
</span>
</td>
<td>
3.1<span class="separator">/</span>5.5<span class="separator">/</span>21.4
<span class="dimmed">
(<span class="green">4.45</span>)
</span>
</td>
<td>
<div id="partner-86729-edit-note" phx-hook="PartnerEditHook" class="partner__note">
alguem quer jogar normal game
</div>
</td>
<td class="partner__inserted" title="2021-05-19 12:33:38">
3 minutos atrás
<div class="opacity-25 group-hover:opacity-100 hover:text-red-600 transition-opacity" phx-click="report" phx-target="1">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 inline-block" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
</svg>
</div>
</td>
</tr>
</tbody>
</table>
我尝试了下面的代码但没有成功:
const $ = cheerio.load(html);
$('body > #partners > tbody > tr > td').toArray().map(item => {
console.log($(item).text());
});
解决方法
那就是:
$('td:nth-of-type(1)').get().map(td => $(td),text())