如何保存Cookie并将其加载到另一个伪造的会话中? [英] How to save cookies and load it in another puppeteer session?

查看:177
本文介绍了如何保存Cookie并将其加载到另一个伪造的会话中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中,我必须两次请求相同的网页才能在第一个请求中获取cookie并在第二个请求中使用它。

I had to request the same webpage twice to get the cookies in the 1st request and use it in the 2nd request in the following example.

有人可以告诉我吗?在一个伪造者会话中保存cookie并在另一个会话中加载它的代码,从而无需在第二个会话中两次请求相同的网页?谢谢。

Could anybody show me the code to save the cookies in one puppeteer session and load it in another session so that there is no need to request the same webpage twice in the 2nd session? Thanks.

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.genecards.org/cgi-bin/carddisp.pl?gene=BSCL2');
    await page.goto('https://www.genecards.org/cgi-bin/carddisp.pl?gene=BSCL2');

    const linkHandlers = await page.$x("//div[@id='enhancers']//a[@data-track-event='Table See-All']");

    if (linkHandlers.length > 0) {
        const [response] = await Promise.all([
            page.waitForResponse(response => response.url().includes('/gene/api/data/Enhancers')),
            linkHandlers[0].click()
        ]);
        const resp_text = await response.text();
        console.log(resp_text);
    } else {
        throw new Error("Link not found");
    }
    await browser.close();
})();


推荐答案

要保存cookie,可以使用< a href = https://github.com/GoogleChrome/puppeteer/blob/v1.17.0/docs/api.md#pagecookiesurls rel = noreferrer> page.cookies 。要重用cookie,可以使用 page.setCookies 函数。

To save the cookies, you can use the function page.cookies. To reuse the cookies, you can use the page.setCookies function.

将cookie保存到磁盘 >

const fs = require('fs').promises;

// ... puppeteer code
const cookies = await page.cookies();
await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));

这将读取当前URL的cookie并通过将其保存到磁盘JSON.stringify fs.writeFile

This will read the cookies for the current URL and save them to disk via JSON.stringify and fs.writeFile.

重复使用Cookie

const fs = require('fs').promises;

// ... puppeteer code
const cookiesString = await fs.readFile('./cookies.json');
const cookies = JSON.parse(cookiesString);
await page.setCookie(...cookies);

要重新使用Cookie,请通过 fs.readFile从磁盘读取文件。然后通过 JSON.parse 解析文件内容。之后,您必须调用 page.setCookie 函数。由于函数期望将cookie作为参数(而不是cookie的一个数组参数),因此我们依赖传播运算符,它允许使用给定的 cookies setCookie 函数c $ c>数组作为单独的参数。

To reuse the cookies, read the files from the disk via fs.readFile. Then parse the file content via JSON.parse. After that you have to call the page.setCookie function. As the function expects the cookies as arguments (and not as one array argument with cookies), we rely on the spread operator, which allows to call the setCookie function with the given cookies array as individual arguments.

这篇关于如何保存Cookie并将其加载到另一个伪造的会话中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆