使用cheerio,$
被定义为cheerio对象,我试图从html中具有类forceWordWrap
的一些元素中获取文本。以下cheerio选择器没有返回任何内容。我究竟做错了什么?感谢
const text = $("td[class='forceWordWrap']");
const date = text.eq(0).text();
const title = text.eq(1).text();
const description = text.eq(2).text();
<p/>
<form name="his" method="post" action="/a/b.axp">
<input type="hidden" name="action" value="accept"/>
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr class="rowHeading">
<td width="14%">
<div align="left" style="white-space:nowrap">
going home
</div>
</td>
<td width="86%">
<div align="left">
say bye.
</div>
</td>
</tr>
<tr class="rowLight">
<td class="boldBodyText forceWordWrap">
<input type="hidden" name="king" value="Tut"/>
the kings
</td>
<td class="boldBodyText forceWordWrap">
Reminder – Please see the king.
</td>
</tr>
<tr class="rowLight">
<td style="white-space:normal"> </td>
<td class="bodyText forceWordWrap">
YOu got to do this.
</td>
</tr>
</table>
答案 0 :(得分:2)
使用
$("td.forceWordWrap");
选择器作为Attribute Equals Selector [name=”value”]
选择具有指定属性的元素,其值完全等于某个值。
const text = $("td.forceWordWrap");
const date = text.eq(0).text();
const title = text.eq(1).text();
const description = text.eq(2).text();
console.log(date);
console.log(title);
console.log(description);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form name="his" method="post" action="/a/b.axp">
<input type="hidden" name="action" value="accept" />
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr class="rowHeading">
<td width="14%">
<div align="left" style="white-space:nowrap">
going home
</div>
</td>
<td width="86%">
<div align="left">
say bye.
</div>
</td>
</tr>
<tr class="rowLight">
<td class="boldBodyText forceWordWrap">
<input type="hidden" name="king" value="Tut" />the kings
</td>
<td class="boldBodyText forceWordWrap">
Reminder – Please see the king.
</td>
</tr>
<tr class="rowLight">
<td style="white-space:normal"> </td>
<td class="bodyText forceWordWrap">YOu got to do this.
</td>
</tr>
</table>
&#13;