如何解决抓取后需要访问img url
我正在Node.js中运行操纵up。当我在一个单独的文件中单独执行异步函数时,它可以很好地获取URL,但是,我的问题是保存该值,以便在完成该操作后将其插入“ bot.on”函数中。
我有这个:
const puppeteer = require("puppeteer");
let imgs;
const test = (async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://inspirobot.me/");
await page.click(".btn-text");
await page.waitFor(1500);
imgs = await page.$$eval(".generator img[src]",(imgs) =>
imgs.map((img) => img.getAttribute("src"))
);
await browser.close();
})();
bot.on("message",async (message) => {
if (message.content === "!quote") {
console.log("finding pics");
const embed = new Discord.MessageEmbed()
.setTitle("INSPIROBOT")
.setThumbnail(
"https://inspirobot.me/website/images/inspirobot-dark-green.png"
)
.setColor(0xff0000)
.setDescription("Your dose of inspiration here:");
.setImage('**this needs to be the img URL but nothing works**')
message.channel.send(embed);
}
});
解决方法
您需要在事件处理程序中调用test
并从那里获取图像:
...
await browser.close();
return page.$$eval(".generator img[src]",(imgs) =>
imgs.map((img) => img.getAttribute("src"))
);
}
调用
if (message.content === "!quote") {
console.log("finding pics");
const imgs = await test();
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。