2016-05-31 3 views
0

cheerioを使用して、$はcheerioオブジェクトとして定義されています。私は、htmlでクラスforceWordWrapを持ついくつかの要素からテキストを取得しようとしています。次のcheerioセレクタは何も返しません。私は間違って何をしていますか? cheerioでhtml要素からテキストを抽出

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="https://stackoverflow.com/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">&nbsp;</td> 
       <td class="bodyText forceWordWrap"> 



        YOu got to do this. 


       </td> 
      </tr> 


     </table> 
+0

あなたは私達にあなたのマークアップを表示する必要があるかもしれませ... – Rayon

+0

はチェリオのように定義 '$'ですか? – str

答えて

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);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<form name="his" method="post" action="https://stackoverflow.com/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">&nbsp;</td> 
 
     <td class="bodyText forceWordWrap">YOu got to do this. 
 
     </td> 
 
    </tr> 
 
    </table>

関連する問題