- 自动会话管理
- 锁定 195 个国家/地区的任意城市
- 无限并发会话
如何在 Puppeteer 中保存和加载 Cookie?
使用 Puppeteer 保存和加载 Cookie 是在多个页面之间有效维持会话状态的强有力方法。这对于网页抓取任务尤其有用,可确保您在不同页面之间导航时不会丢失会话数据。您可使用 page.cookies()
方法检索网页中的所有 Cookie,并使用 page.setCookie()
方法将 Cookie 加载至网页中。如需详细了解什么是 HTTP Cookie,请参阅此篇博文。以下示例详细展示了使用 Puppeteer 保存和加载 Cookie 具体操作步骤:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Open the first website and wait for it to load
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
// Save all cookies from the first page
const cookies = await page.cookies();
console.log('Cookies from example.com:', cookies);
// Open a second website and wait for it to load
await page.goto('https://httpbin.org/cookies', { waitUntil: 'networkidle2' });
// Load the previously saved cookies into the second page
await page.setCookie(...cookies);
// Get and log the cookies from the second page to verify they were set
const cookiesSet = await page.cookies();
console.log('Cookies set in httpbin.org:', cookiesSet);
await browser.close();
})();
代码解释
- 启动 Puppeteer 并打开新页面:
const browser = await puppeteer.launch(); const page = await browser.newPage();
- 导航至第一个网站并等待其完全加载:
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
其中的waitUntil: 'networkidle2'
选项可确保页面完全加载后才继续。 - 检索并记录第一个网站的 Cookies:
const cookies = await page.cookies(); console.log('Cookies from example.com:', cookies);
- 导航至第二个网站并等待其完全加载:
await page.goto('https://httpbin.org/cookies', { waitUntil: 'networkidle2' });
- 将已保存的 Cookies 加载至第二个网站:
await page.setCookie(...cookies);
- 检索并记录第二个网站的 Cookies 以进行验证:
const cookiesSet = await page.cookies(); console.log('Cookies set in httpbin.org:', cookiesSet);
- 关闭浏览器:
await browser.close();
按照上述步骤进行操作后,您即可使用 Puppeteer 在不同网页之间正确保存和加载 cookie。此方法让您在网页抓取任务期间有效维持会话状态。
如需更高级的网页抓取解决方案,请考虑使用 Bright Data 的 Puppeteer 浏览器。它提供一体化解决方案,用于处理自动解锁基础架构、绕过验证码并让您轻松扩展抓取项目。
获得全球 超20000 位客户的信赖
TRUSTED BY 超20000 CUSTOMERS WORLDWIDE
欢迎来到 云抓取