Как вычеркнуть число из `<span>` без ID или класса? - PullRequest
0 голосов
/ 18 апреля 2020

Моя цель - соскрести с сайта. Я использую 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>

Я хочу получить данные 49.82 в теге <span>. Но нет имени класса или идентификатора. Как я могу это сделать?

1 Ответ

0 голосов
/ 18 апреля 2020

Вы можете сделать:

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