我正在尝试从需要授权的网站下载pdf。我已经在邮递员中对此进行了测试,成功完成响应所需要做的就是:
问题是在Postman中,我不需要输入授权信息即可获得成功的响应,但是在我的脚本中,我得到的响应始终是错误的登录信息。另外,有多个Cookie要发送,所以这可能是我的问题。
这是我的代码。我该如何进行这项工作?
const puppeteer = require("puppeteer");
const fs = require("fs");
const fetch = require("node-fetch");
let scrape = async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
let login = "example";
let pwd = "password";
await page.goto(
"example.com"
);
await page.type("#LMDP_Spi_tmp", login);
await page.type("#LMDP_Password_tmp", pwd);
await page.click("#fg_lmdp_pwd > div > div.col-xs-5.col-sm-4 > button");
await page.waitFor(1000);
await page.goto("second/page/path");
await page.waitForSelector(
"body > table:nth-child(2) > tbody > tr:nth-child(4) > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(5) > a"
);
await page.click(
"body > table:nth-child(2) > tbody > tr:nth-child(4) > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(5) > a > img"
);
const downloadUrl = "IFRAME URL"
let sessionCookie = (await page.cookies());
const downloadUserData = async sessionCookie => {
console.log("requesting data");
const res = await fetch(downloadUrl, {
headers: {
method: "GET",
Cookie: sessionCookie,
credentials: 'same-origin',
Accept: 'text/html'
}
}).then(res => res.text())
.then(body => console.log(body));
let pdfURL = //something I need to grab from the iFrame's src!
const pdf = await fetch(pdfUrl, {
headers: {
method: "GET",
Cookie: sessionCookie
}
})
fs.writeFile("doc.pdf", pdf, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
})
};
return await downloadUserData(sessionCookie);
};
scrape();