-1
I’m creating an application to download an Instagram image through the URL and I’m using the package puppeteer
to accomplish this task.
Within the call of the method evaluate
(method to run a JS code on the page) I try to get and return an image element.
The problem is that whenever I try to get the image, I end up getting null
as a result. See my code below:
const puppeteer = require("puppeteer");
const url = "https://www.instagram.com/p/<image_code>/";
async function getImageFrom(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const image = await page.evaluate(function() {
return document.querySelector(".KL4Bh > img");
});
await browser.close();
return image;
}
getImageFrom(url).then((image) => {
console.log(image);
});
What am I doing wrong? I’ve tried using querySelectorAll
searching only for elements with the tag <img>
but it also returns me only an empty array.
Edit: I don’t know what happened, but the method is returning undefined
now.
This selector (
.KL4Bh > img
) doesn’t seem very reliable and long-lasting... It’s very typical of Facebook to change this type of selector with a certain frequency. Despite this, I don’t think that’s the problem... Besides, what would it beNone
? :P– Luiz Felipe
Oops, I was wrong I mistook
null
withNone
kkk (Python xD mania). Good if Instagram does this I don’t know, but I tested pick up the element right on the browser console and it worked. Also, I also tested get only the elements of<img>
as I had already said in the reply and yet thepuppeteer
only returned an empty array.– JeanExtreme002
From what I can see here, it doesn’t seem to be a problem with Puppeteer. Instagram inserts the image via Javascript. If you try to download HTML using some tool like Curl, you will see that the selector you entered is not there. I think it is placed via Javascript. I do not know if the method
evaluate
expects that all the Javascripts of the page will be properly completed, but it seems to me that this is not the case. Worth investigating this.– Luiz Felipe
But the goal of
puppeteer
as far as I understand it is to be a kind ofselenium
where it renders the page to be manipulated. So even if the images are added via Javascript, thepuppeteer
should render this and return me the elements.– JeanExtreme002