我目前正在尝试从this URL中解析一些超文本标记语言:
我要查找的主要信息是列出的Weight。使用Chrome中的控制台,我可以发出以下命令:
$("th:contains(Weight)").parent()[0];它将为我提供包含所需的有关权重的所有信息的表行。
我试图在Cheerio中使用它,但它只返回undefined。这是我的Node.js代码:
var needle = require('needle');
var cheerio = require('cheerio');
function rei(product) {
//Request page from rei.com and follow the redirect
return needle("get", "https://rei.com/product/" + product, {
follow_max: 5
}).then(function(response) {
var $ = cheerio.load(response.body);
var test = $("th:contains(Weight)").parent()[0];
console.log(test);
}).catch(function(error) {
console.log(error);
})
};
rei(893905);从Rei的网站上自动获取我需要的信息的最佳方式是什么?
发布于 2018-07-18 01:09:07
试试这个:
var needle = require('needle');
var cheerio = require('cheerio');
var fs = require('fs');
function rei(product) {
//Request page from rei.com and follow the redirect
return needle("get", "https://rei.com/product/" + product, {
follow_max: 5
}).then(function(response) {
var $ = cheerio.load(response.body);
// your data in script
var content = $('script[data-client-store="product-details"]').html();
content = JSON.parse(content);
for (var spec of content.specs) {
if (spec.name == 'Weight') {
console.log(spec.values)
}
}
}).catch(function(error) {
console.log(error);
})
};
rei(893905);https://stackoverflow.com/questions/50686830
复制相似问题