I'm working with Web Scraping using puppeteer, iterating with DOM elements for example:
<div class="entry">
<div class="information info_0"> <p> Close </p> </div>
<div class="details"> bla bla bla bla </div>
<div class="slide"> <img src="0_path1" /> </div>
<div class="slide"> <img src="1_path2" /> </div>
<div class="slide"> <img src="2_path3" /> </div>
<div class="slide"> <img src="3_path4" /> </div>
<div class="slide"> <img src="4_path5" /> </div>
<div class="information info_1"> <p> Rextry </p> </div>
<div class="details"> ble ble ble </div>
<div class="slide"> <img src="1_path1" /> </div>
<div class="slide"> <img src="1_path2" /> </div>
<div class="slide"> <img src="1_path3" /> </div>
</div>
so I have to wait this entry load so, waitForSelector('div.entry').
const result = await page.evaluate(() => {
... Iterate Images ...
}
Iterate Images:
const items = document.querySelectorAll('entry');
let information = []
let details = []
items.forEach(function(el) {
el.querySelectorAll('div.information p').forEach(function(el) {
information.push(el.innerHTML)
}
el.querySelectorAll('div.details').forEach(function(el) {
details.push(el.innerHTML)
}
}
now I could create an object for these array:
let ob = []
information.forEach((el, index) => {
ob.push({
info: information[index],
detail: details[index],
});
});
result:
[
{ info: 'Close', details: 'bla bla bla bla bla' },
{ info: 'Rextry', details: 'ble ble ble' },
]
But now I would to like to associate the images src with respective information. How could I do that?
I thought iterate images and push inside info array:
el.querySelectorAll('div.slide').forEach(function(el) {
info.push(el.innerHTML)
}
Results would be:
info: ['Close','0_path1',0_path2',0_path3',0_path4',0_path5','Rextry','1_path1','1_path2','1_path3']