Node.js 木偶师和 Cheerio Div Table 抓取
Node.js Puppeteer & Cheerio Div Table Scraping
我一直在使用 puppeteer 和 cheerio 开发 node.js 爬虫,但在提取一些 div table 信息时遇到问题。我需要拉水果和蔬菜 table table 但不是肉 table 并且所有 3 个并不总是存在。
<div class="specs__title">
<h4>Fruit</h4>
</div>
<div class="specs__table">
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Apples</div>
<div class="col-6 specs__cell">4lbs</div>
</div>
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Grapes</div>
<div class="col-6 specs__cell">3lbs</div>
</div>
</div>
<div class="specs__title">
<h4>Vegetables</h4>
</div>
<div class="specs__table">
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Carrots</div>
<div class="col-6 specs__cell">7lbs</div>
</div>
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Corn</div>
<div class="col-6 specs__cell">5lbs</div>
</div>
</div>
<div class="specs__title">
<h4>Meat</h4>
</div>
<div class="specs__table">
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Turkey</div>
<div class="col-6 specs__cell">2lbs</div>
</div>
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Beef</div>
<div class="col-6 specs__cell">1lb</div>
</div>
</div>
如有任何帮助,我们将不胜感激。
它应该看起来像这样:(未测试)
$('h4:contains("Fruits"),h4:contains("Vegetables")').map((i, h4) => {
return $(h4).parent().find('+ .specs__table').html()
}).get()
我不确定这是否是最好的方法,但这就是我的工作方式。
for (let i = 0; i < 3; i++) {
if($('#specsContainer > div.specs__title > h4', html).eq(i).text() == "Fruits"){
console.log($('#specsContainer > div.specs__table', html).eq(i).html());
};
if($('#specsContainer > div.specs__title > h4', html).eq(i).text() == "Vegetables"){
console.log($('#specsContainer > div.specs__table', html).eq(i).html());
};
};
我一直在使用 puppeteer 和 cheerio 开发 node.js 爬虫,但在提取一些 div table 信息时遇到问题。我需要拉水果和蔬菜 table table 但不是肉 table 并且所有 3 个并不总是存在。
<div class="specs__title">
<h4>Fruit</h4>
</div>
<div class="specs__table">
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Apples</div>
<div class="col-6 specs__cell">4lbs</div>
</div>
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Grapes</div>
<div class="col-6 specs__cell">3lbs</div>
</div>
</div>
<div class="specs__title">
<h4>Vegetables</h4>
</div>
<div class="specs__table">
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Carrots</div>
<div class="col-6 specs__cell">7lbs</div>
</div>
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Corn</div>
<div class="col-6 specs__cell">5lbs</div>
</div>
</div>
<div class="specs__title">
<h4>Meat</h4>
</div>
<div class="specs__table">
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Turkey</div>
<div class="col-6 specs__cell">2lbs</div>
</div>
<div class="specs__group col-12 col-lg-6">
<div class="col-6 specs__cell specs__cell--label">Beef</div>
<div class="col-6 specs__cell">1lb</div>
</div>
</div>
如有任何帮助,我们将不胜感激。
它应该看起来像这样:(未测试)
$('h4:contains("Fruits"),h4:contains("Vegetables")').map((i, h4) => {
return $(h4).parent().find('+ .specs__table').html()
}).get()
我不确定这是否是最好的方法,但这就是我的工作方式。
for (let i = 0; i < 3; i++) {
if($('#specsContainer > div.specs__title > h4', html).eq(i).text() == "Fruits"){
console.log($('#specsContainer > div.specs__table', html).eq(i).html());
};
if($('#specsContainer > div.specs__title > h4', html).eq(i).text() == "Vegetables"){
console.log($('#specsContainer > div.specs__table', html).eq(i).html());
};
};