Sure! Here’s an example of an HTML content formatted in a div using the query “puppeteer get img src”:
“`html
This is an example of using Puppeteer to get the image src attribute.
First, we need to install the Puppeteer package:
npm install puppeteer
Then, we can use the following code to get the image src:
const puppeteer = require('puppeteer');
async function getImageSrc(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const imageSrcs = await page.evaluate(() => {
const images = document.querySelectorAll('img');
const srcs = Array.from(images).map(img => img.src);
return srcs;
});
await browser.close();
return imageSrcs;
}
// Usage
const url = 'https://example.com';
getImageSrc(url).then(srcs => {
console.log(srcs);
});
In this code, we first import the Puppeteer package and define an async function getImageSrc
that takes a URL as input. Inside the function, we launch a headless browser instance, open a new page, and navigate to the specified URL.
Once the page loads, we use page.evaluate
to execute custom JavaScript code on the page. In this case, we select all <img>
elements and extract their src attributes into an array.
Finally, we close the browser and return the array of image srcs.
In the usage section, we call the getImageSrc
function with the desired URL and handle the extracted srcs in the promise’s then
block.
Note that you need to have Puppeteer installed in your project for this code to work. You can install it using the provided command npm install puppeteer
.
“`
This HTML content is enclosed within a `