【发布时间】: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>
我想在<span> 标签中获取数据49.82。但是没有类名或id。我该怎么做?
【问题讨论】:
-
你现在的JS代码不行吗?有没有抛出任何错误?你试过不同的选择器吗?
-
没有错误,也没有检索到任何内容
标签: javascript html node.js cheerio