2016-10-10 65 views
0

試圖刮掉此頁面上的價格因素:Cheerio/Node.js的 - 獲取元素

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()); 

    }) 

回答

0

current-price我在HTML看到是這樣的:

<div class="current-price"> 

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

您需要:

$('div.current-price') 

或只是

$('.current-price') 
+0

嗯,似乎仍不是殺我。下面是我想要報廢的元素,看起來仍然在之內,但我可能會漏掉一些東西。 ' $ 49.00' – kmpace

+0

@kmpace - 我在您引用的頁面的原始HTML中看不到那個元素。一旦頁面加載到瀏覽器中,它就在DOM中,但不在頁面的原始源代碼中,因此它必須通過Javascript添加。 Cheerio只查看RAW HTML,因此看不到它。 – jfriend00