extracting text from html elements with cheerio

早过忘川 提交于 2019-12-25 07:19:07

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/37537320/extracting-text-from-html-elements-with-cheerio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!