Javascript 在nodejs中使用cheerio进行网页抓取?
我正在尝试在NodeJS中使用cheerio和http进行web抓取 html代码的一部分:Javascript 在nodejs中使用cheerio进行网页抓取?,javascript,node.js,http,web-scraping,cheerio,Javascript,Node.js,Http,Web Scraping,Cheerio,我正在尝试在NodeJS中使用cheerio和http进行web抓取 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" cla
<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"> </span> 585.00</span>
</td>
</tr>
但同样:
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"> </span> 329.00</span>
</td>
</tr>
它没有给出输出。您使用了错误的id。。。它应该是
priceblock\u 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指向第一个
元素,但您需要以第二个
元素为目标,因此请使用
“#priceblock_ourprice”因为在第一个示例中,id是正确的,它指示指向您的价格的范围,而且如果替换id不起作用,请尝试使用id并获取其子项。您可以参考一些教程,在那里我找到如何使用子项,然后使用下一个etcdid完成任务,还是还需要帮助?这是我的邮件苏拉布。reddy2k14@gmail.com,加上我的闲逛
var mrp = '#priceblock_ourprice_lbl';
scraper(mrp).filter(function(){
var data_mrp = scraper(this);
console.log(data_mrp.text());
scraped = scraped + data_mrp.text()+';';
});
var mrp = '#priceblock_ourprice';
scraper(mrp).filter(function(){
var data_mrp = scraper(this);
console.log(data_mrp.text());
scraped = scraped + data_mrp.text()+';';
});