本文介绍了从带有cheerio的html元素中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用cheerio,$
被定义为cheerio对象,我试图从html中具有类forceWordWrap
的某些元素中获取文本.以下cheerio选择器未返回任何内容.我究竟做错了什么?谢谢
Using cheerio, $
is defined as cheerio object, I am trying to get the text from some elements which have a class forceWordWrap
in a html. The following cheerio selectors are returning nothing. What am I doing wrong? Thanks
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>
推荐答案
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);
<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>
这篇关于从带有cheerio的html元素中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!