从带有cheerio的html元素中提取文本 [英] extracting text from html elements with cheerio

查看:215
本文介绍了从带有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">&nbsp;</td>
              <td class="bodyText forceWordWrap">



                    YOu got to do this.


              </td>
            </tr>


      </table>

推荐答案

使用$("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="/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>

这篇关于从带有cheerio的html元素中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆