Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

javascript - extracting text from html elements with 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">&nbsp;</td>
              <td class="bodyText forceWordWrap">



                    YOu got to do this.


              </td>
            </tr>


      </table>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use $("td.forceWordWrap"); selector as Attribute Equals Selector [name=”value”] selects elements that have the specified attribute with a value exactly equal to a certain 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="/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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...