Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使用cheerio获取子属性值_Javascript_Html_Node.js_Cheerio - Fatal编程技术网

如何在javascript中使用cheerio获取子属性值

如何在javascript中使用cheerio获取子属性值,javascript,html,node.js,cheerio,Javascript,Html,Node.js,Cheerio,我正在使用cheerio库进行数据抓取。我正在尝试使用下面的方法获取标签的值 var sparkLine = $(this) .find("td") .eq(7).text; HTML HTML -4.85% 将此代码应用于问题的两个TD标签: $('td', htmlCode) .each(function (counter, elem) { console.log(`#${counter}`); console

我正在使用cheerio库进行数据抓取。我正在尝试使用下面的方法获取标签的值

var sparkLine = $(this)
  .find("td")
  .eq(7).text;
HTML

HTML

-4.85%

将此代码应用于问题的两个TD标签:

    $('td', htmlCode)
        .each(function (counter, elem) {
            console.log(`#${counter}`);
            console.log($(this).html());
        });
<td>
   <a href="/currencies/bitcoin/#charts">
     <img class="sparkline"
          alt="sparkline" 
          src="https://files.coinmarketcap.com/generated/sparklines/1.png">
   </a>
</td>

// Returns:
#0
<a href="/currencies/bitcoin/#charts">
  <img class="sparkline"
         alt="sparkline" 
         src="https://files.coinmarketcap.com/generated/sparklines/1.png">
</a>
第一个标签:

    $('td', htmlCode)
        .each(function (counter, elem) {
            console.log(`#${counter}`);
            console.log($(this).html());
        });
<td>
   <a href="/currencies/bitcoin/#charts">
     <img class="sparkline"
          alt="sparkline" 
          src="https://files.coinmarketcap.com/generated/sparklines/1.png">
   </a>
</td>

// Returns:
#0
<a href="/currencies/bitcoin/#charts">
  <img class="sparkline"
         alt="sparkline" 
         src="https://files.coinmarketcap.com/generated/sparklines/1.png">
</a>

//返回:
#0
**二,。第二个标签:**

<td class="no-wrap percent-24h negative_change text-right"
    data-usd="-4.85"
    data-btc="0.00" >
    -4.85%
</td>

// Returns:
#0
-4.85%

-4.85%
//返回:
#0
-4.85%

将此代码应用于问题的两个TD标签:

    $('td', htmlCode)
        .each(function (counter, elem) {
            console.log(`#${counter}`);
            console.log($(this).html());
        });
<td>
   <a href="/currencies/bitcoin/#charts">
     <img class="sparkline"
          alt="sparkline" 
          src="https://files.coinmarketcap.com/generated/sparklines/1.png">
   </a>
</td>

// Returns:
#0
<a href="/currencies/bitcoin/#charts">
  <img class="sparkline"
         alt="sparkline" 
         src="https://files.coinmarketcap.com/generated/sparklines/1.png">
</a>
第一个标签:

    $('td', htmlCode)
        .each(function (counter, elem) {
            console.log(`#${counter}`);
            console.log($(this).html());
        });
<td>
   <a href="/currencies/bitcoin/#charts">
     <img class="sparkline"
          alt="sparkline" 
          src="https://files.coinmarketcap.com/generated/sparklines/1.png">
   </a>
</td>

// Returns:
#0
<a href="/currencies/bitcoin/#charts">
  <img class="sparkline"
         alt="sparkline" 
         src="https://files.coinmarketcap.com/generated/sparklines/1.png">
</a>

//返回:
#0
**二,。第二个标签:**

<td class="no-wrap percent-24h negative_change text-right"
    data-usd="-4.85"
    data-btc="0.00" >
    -4.85%
</td>

// Returns:
#0
-4.85%

-4.85%
//返回:
#0
-4.85%