如何从没有 ID 或 class 的 `<span>` 中抓取数字?
How to scrape a number from a `<span>` without an ID or class?
我的目标是从网站上抓取。我正在使用 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>
我想获取 <span>
标签中的数据 49.82
。但是没有 class 名称或 ID。我该怎么做?
你可以这样做:
$('p:contains("NR Grid Frequency") span').text()
我的目标是从网站上抓取。我正在使用 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>
我想获取 <span>
标签中的数据 49.82
。但是没有 class 名称或 ID。我该怎么做?
你可以这样做:
$('p:contains("NR Grid Frequency") span').text()