簡體   English   中英

在 nodejs 中使用 cheerio 進行網絡抓取?

[英]web scraping using cheerio in nodejs?

我正在嘗試在節點 js 中使用 cheerio 和 http 進行網絡抓取

部分html代碼:

<tr>
    <td id="priceblock_saleprice_lbl" class="a-color-price a-size-base a-text-right a-nowrap">Sale:</td>

    <td class="a-span12">
    <span id="priceblock_saleprice" class="a-size-medium a-color-price"><span class="currencyINR">&nbsp;&nbsp;</span> 585.00</span>

    </td>
</tr>

節點代碼:

var sale_price = '#priceblock_saleprice';
            scraper(sale_price).filter(function(){
            var data_price = scraper(this);
            console.log(data_price.text());
            scraped = scraped + data_price.text()+';';
          });

 this code is giving 585 as output.

但同樣:

部分html頁面:

<tr id="priceblock_ourprice_row">
    <td id="priceblock_ourprice_lbl" class="a-color-secondary a-size-base a-text-right a-nowrap">Price:</td>
    <td class="a-span12">
        <span id="priceblock_ourprice" class="a-size-medium a-color-price"><span class="currencyINR">&nbsp;&nbsp;</span> 329.00</span>
    </td>
</tr>

節點代碼:

var mrp  = '#priceblock_ourprice_lbl';
scraper(mrp).filter(function(){
            var data_mrp = scraper(this);
            console.log(data_mrp.text());
            scraped = scraped + data_mrp.text()+';';
          });

它沒有給出輸出。

你用錯了 id...應該是priceblock_ourprice

var mrp  = '#priceblock_ourprice';
scraper(mrp).filter(function(){
   var data_mrp = scraper(this);
   console.log(data_mrp.text());
   scraped = scraped + data_mrp.text()+';';
});

第二個代碼片段中使用的 id 指向第一個<td>元素,但您需要定位第二個<td>元素,因此使用“#priceblock_ourprice”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM