Skip to content
Advertisement

pinterest web scrape image

I’m trying to get a url from a pinterest image and send a url of it through the general profile of the user on pinterest, but it’s returning me undefined

enter image description here

my code:

const Command = require("../../structures/Command");
const cheerio = require("cheerio");
const rp = require("request-promise");
const { head } = require("request");

module.exports = class Pinterest extends Command {
  constructor(client) {
    super(client);
    this.client = client;

    this.name = "pinterest";
    this.category = "Dono";
    this.aliases = [];

    this.enabled = true;
    this.guildOnly = true;
  }
  async run({ message, args, prefix, author }, t) {
    if (
      message.author.id !== "196679829800747017"
    )
      return;

      const URL = (`https://br.pinterest.com/n1cotin3/_created/`)
      const headerObj = {
          uri: URL
      };
      rp(headerObj)
      .then(html => {
          var $ = cheerio.load(html)

          const avatar = $("#mweb-unauth-container > div > div:nth-child(2) > div:nth-child(3) > div.F6l.ZZS.k1A.zI7.iyn.Hsu > div > div > div > div:nth-child(1) > div:nth-child(1) > div > div > div > div > div > a > div > div > div > div > div.XiG.zI7.iyn.Hsu > img").attr("src")
          console.log(avatar)
    message.react(`💥`);
  })
}
};

Advertisement

Answer

The problem is that the page is still loading. #mweb-unauth-container > div > div:nth-child(2) doesn’t exist, because #mweb-unauth-container > div only has one div child, and it’s a loading icon. I don’t think this is something you can do with cheerio, you’ll have to use an alternative that can resolve Javascript (such as Puppeteer).

Alternatively, if you don’t want to scrape, you can use a private API (which, although subject to change at any point, would definitely be more performant):

https://widgets.pinterest.com/v3/pidgets/users/n1cotin3/pins/

Example:

const res = await requestThatEnpointSomehow();
const images = res.data.pins.map(({ images }) => images['564x']);

// `images` will be a list of URLs.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement