【问题标题】:Cheerio / Node.JS - retrieving an elementCheerio / Node.JS - 检索元素
【发布时间】:2017-02-19 08:15:15
【问题描述】:

正在尝试抓取此页面上的价格元素:

http://us.asos.com/asos/asos-bomber-jacket-in-khaki/prd/5773457?iid=5773457&affid=14174&channelref=product%20search&mk=abc&currencyid=2&gclid=CIvXk9DEt88CFYpehgodLo0Ivg

运行console.log 时无法得到任何东西,有什么建议吗?我已经检查过我选择了正确的元素。

var request = require('request');
var cheerio = require('cheerio');

var url = 'http://us.asos.com/asos/asos-bomber-jacket-in-khaki/prd/5773457?iid=5773457&affid=14174&channelref=product%20search&mk=abc&currencyid=2&gclid=CIvXk9DEt88CFYpehgodLo0Ivg';

request(url , function(error, response, html) {
if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html); 


    $('span.current-price').each(function(i, element){
        console.log($(this).text());

    })

【问题讨论】:

    标签: javascript jquery node.js web-scraping cheerio


    【解决方案1】:

    我在 HTML 中看到的current-price 是这样的:

    <div class="current-price">
    

    所以,$('span.current-price') 将不匹配。

    你需要:

    $('div.current-price')
    

    或者只是

    $('.current-price')
    

    【讨论】:

    • 嗯,对我来说似乎仍然没有报废。下面是我试图废弃的元素,看起来它仍在 内,但我可能会遗漏一些东西。 &lt;span data-id="current-price" data-bind="text: priceText(), css: {'product-price-discounted' : isDiscountedPrice }" class="current-price"&gt;$49.00&lt;/span&gt;
    • @kmpace - 我在您引用的页面的原始 HTML 中看不到该元素。一旦页面加载到浏览器中,它就在 DOM 中,但不在页面的原始源中,所以它必须通过 Javascript 添加。 Cheerio 只查看 RAW HTML,所以它看不到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2019-09-18
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多