我正在尝试使用 cheerio 解析代码并在 Node Js 上请求,但出现错误 undefined
我已经检查过了,这不是请求错误,它是 cheerio
这是我的解析代码的一部分。
const options = Object.assign({
url: buildUrl(opts),
followAllRedirects: true
}, opts.requestOptions);
request(options, opts.throttle)
.then(cheerio.load)
.then(parseFields)
.then(function (app) {
resolve(app);
})
.catch(reject);
});
}
function parseFields ($) {
const h2 = $('faq_cat').attr('id')
const fields = {
h2
};
我要解析的内容
<div class="faq_cat" id="faq_box_faq2">
谢谢大家!)
Express 服务器应用程序代码:
const express = require('express')
const app = express()
var gplay = require('google-play-scraper');
gplay.download({downloadId: 'air.com.helloair.HELLOFROG',
nameid: 'digital-world-digimons'})
.then(console.log, console.log);
app.listen(3000, () => console.log('Example app listening on port 3000!'))
使用 console.log($.html());
最佳答案
您的选择器缺少 。
现在您正在寻找一个名为 faq_cat
的标签,它不存在。您想要选择具有类名 faq_cat
使用 const h2 = $('.faq_cat').attr('id')
关于javascript - Node js 上的 Cheerio 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50293114/