Node.js Puppeteer&Cheerio Div Table刮痧

问题描述 投票:0回答:2

我一直在使用puppeteer和cheerio处理node.js scraper,但是我在解决一些div表信息时遇到了问题。我需要拉水果和蔬菜桌而不是肉桌,所有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>

任何帮助,将不胜感激。

html node.js web-scraping puppeteer cheerio
2个回答
0
投票

它应该看起来像这样:(未经测试)

$('h4:contains("Fruits"),h4:contains("Vegetables")').map((i, h4) => {
  return $(h4).parent().find('+ .specs__table').html()
}).get()

0
投票

我不确定这是否是最好的方法,但这就是我的工作方式。

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());
	};
};
© www.soinside.com 2019 - 2024. All rights reserved.