<dl class="col-sm-12 col-md-4"> <dt> <a title="Details for Angela Contat"…
- User
- Assistant
You can use the
page.evaluate()function to run JavaScript code in the context of the page and extract the desired data.Here's an example code snippet that loops over the
dlelements and extracts thetextContentof thedtandddelements:javascriptconst dls = await page.$$('dl'); for (const dl of dls) { const dt = await dl.$('dt'); const dd = await dl.$('dd'); const dtText = await page.evaluate((element) => element.textContent, dt); const ddText = await page.evaluate((element) => element.textContent, dd); console.log(dtText, ddText); }This code uses the
$$()method to select alldlelements on the page and then loops over eachdlelement. For eachdl, it uses$()to select thedtandddelements and then usespage.evaluate()to extract theirtextContent. The extracted text is then printed to the console. - User
- Assistant
To loop over the
dltags and extract thetextContentof thedtandddtags using Puppeteer, you can use the following code:javascriptconst dlTags = await page.$$('dl'); // select all dl tags for (const dlTag of dlTags) { // loop over the dl tags const dtTag = await dlTag.$('dt a'); // select the a tag under dt tag const ddTag = await dlTag.$('dd'); // select the dd tag under the dl tag const dtText = await page.evaluate((element) => element.textContent.trim(), dtTag); // extract the text content of a tag const ddText = await page.evaluate((element) => element.textContent.trim(), ddTag); // extract the text content of dd tag console.log(dtText, ddText); // do something with the extracted text content }