首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用"contains“选择器时,Cheerio返回undefined

当使用"contains“选择器时,Cheerio返回undefined
EN

Stack Overflow用户
提问于 2018-06-05 02:37:17
回答 1查看 442关注 0票数 0

我目前正在尝试从this URL中解析一些超文本标记语言:

我要查找的主要信息是列出的Weight。使用Chrome中的控制台,我可以发出以下命令:

代码语言:javascript
复制
$("th:contains(Weight)").parent()[0];

它将为我提供包含所需的有关权重的所有信息的表行。

我试图在Cheerio中使用它,但它只返回undefined。这是我的Node.js代码:

代码语言:javascript
复制
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的网站上自动获取我需要的信息的最佳方式是什么?

EN

回答 1

Stack Overflow用户

发布于 2018-07-18 01:09:07

试试这个:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50686830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档