我试图保存存储在帐户网络空间的图像。使用凭据访问私有空间并访问图像链接对于Puppeteer
来说都是可以的。但是图像的src
属性提供了一个URL,其中包含额外的身份验证查询字符串。url可以直接在浏览器中使用,也可以使用page.evaluate()
命令。两者都打开一个文件选择器来下载图像。但是用page.goto()
,错误说我不能导航无效的URL。我尝试了所有的下载方法,我发现,但没有工作!我如何用这种url下载Puppeteer
的图像?
在没有帐户中所有认证的情况下访问图像的URL,我试图隔离问题,只从上面的URL下载图像。但不幸的是,什么都没用。
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({executablePath: '/usr/bin/brave-browser', headless: false});
const page = await browser.newPage();
await page.goto('https://s3.amazonaws.com/spypoint-production-account-failover/5c9f7dc7267fc300f968bb01/60632085b8683500145706d1/20230213/PICT1853_202302131500FHth3.jpg?X-Amz-Expires=86400&X-Amz-Date=20230401T094532Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIATVANQEDJ5KPEZXK2%2F20230401%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=e9be25f01b577c1116b1c5ee00e0a1c1386b716cc7fe0ea19cd4aad77d61684d');
await page.waitForNavigation();
await screenshot();
})();
(我甚至问ChatGPT
,但它没有帮助我…!)非常感谢。
您的url不需要任何东西,所以直接下载图像,您不需要puppeteer为这些url。
let fs = require('fs');
let http = require('https');
let browser;
(async () => {
let url = 'https://s3.amazonaws.com/spypoint-production-account-failover/5c9f7dc7267fc300f968bb01/60632085b8683500145706d1/20230213/PICT1853_202302131500FHth3.jpg?X-Amz-Expires=86400&X-Amz-Date=20230401T094532Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIATVANQEDJ5KPEZXK2%2F20230401%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=e9be25f01b577c1116b1c5ee00e0a1c1386b716cc7fe0ea19cd4aad77d61684d';
function download(url, path) {
http.get(url, (res) => {
res.pipe(fs.createWriteStream(path, {flags: "w+"}));
});
}
download(url, './test.jpg');
})().catch(err => console.error(err)). finally(() => browser ?. close());