【问题标题】:How to scrape a number from a `<span>` without an ID or class?如何在没有 ID 或类的情况下从 `<span>` 中抓取数字?
【发布时间】:2020-07-31 16:27:24
【问题描述】:

我的目标是从网站上抓取。我正在使用 Cheerio 和 Node.js。

这是我的代码:

Main.js:

const request = require('request')
const cheerio = require('cheerio')
const jsonFile = require('jsonfile')
request("https://nrldc.in", function(error, response, html){
  //console.log(response)

  if(!error && response.statusCode == 200){
    const $ = cheerio.load(html)
  }

  let h1 = $('div.textwidget:nth-child(1)> p:nth-child(2) > strong:nth-child(1)> span:nth-child(1)').text()

  console.log(h1)

这是我的 HTML 文件的一部分:

<div class="textwidget">
  <p></p>
  <p style="text-align: left; font-size:24px;font-weight:bold;">NR Grid Frequency: 
    <strong><span style="font-size: 23px;color:red;">49.82</span></strong>
  </p>
</div>

我想在&lt;span&gt; 标签中获取数据49.82。但是没有类名或id。我该怎么做?

【问题讨论】:

  • 你现在的JS代码不行吗?有没有抛出任何错误?你试过不同的选择器吗?
  • 没有错误,也没有检索到任何内容

标签: javascript html node.js cheerio


【解决方案1】:

你可以这样做:

$('p:contains("NR Grid Frequency") span').text()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多