我试图从pinterest图像中获取一个url,并通过pinterest上用户的一般配置文件发送它的一个url,但它正在返回未定义的我

我的代码:
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(``);
})
}
};发布于 2022-05-01 23:31:02
问题是页面仍然在加载。#mweb-unauth-container > div > div:nth-child(2)不存在,因为#mweb-unauth-container > div只有一个div子元素,而且它是一个加载图标。我不认为这是你可以做的事,与欢呼,你将不得不使用一个替代方案,可以解决Javascript (如Puppeteer)。
或者,如果您不想刮,您可以使用一个私有API (尽管它在任何时候都可能发生变化,但肯定会更好地执行):
https://widgets.pinterest.com/v3/pidgets/users/n1cotin3/pins/
示例:
const res = await requestThatEnpointSomehow();
const images = res.data.pins.map(({ images }) => images['564x']);
// `images` will be a list of URLs.https://stackoverflow.com/questions/72081013
复制相似问题