Pey*_*ter 2 automated-tests end-to-end node.js puppeteer puppeteer-cluster
我想知道是否有人使用 Puppeteer-Cluster 可以详细说明 Cluster.Launch({settings}) 如何防止在不同上下文中的页面之间共享 cookie 和 Web 数据。
此处的浏览器上下文是否确实阻止了 cookie,并且不会共享或跟踪用户数据?Browserless' 现在臭名昭著的页面似乎认为不,这里和 .launch({}) 应该在任务上调用,而不是在队列之前。
所以我的问题是,我们如何知道 puppeteer-cluster 是否在排队任务之间共享 cookie/数据?图书馆中有哪些选项可以降低被贴上机器人标签的机会?
设置:我将 page.authenticate 与代理服务、随机用户代理一起使用,但偶尔仍会被我正在执行测试的站点阻止(403)。
async function run() {
// Create a cluster with 2 workers
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_BROWSER, //Cluster.CONCURRENCY_PAGE,
maxConcurrency: 2, //5, //25, //the number of chromes open
monitor: false, //true,
puppeteerOptions: {
executablePath,
args: [
"--proxy-server=pro.proxy.net:2222",
"--incognito",
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-first-run",
"--no-sandbox",
"--no-zygote"
],
headless: false,
sameDomainDelay: 1000,
retryDelay: 3000,
workerCreationDelay: 3000
}
});
// Define a task
await cluster.task(async ({ page, data: url }) => {
extract(url, page); //call the extract
});
//task
const extract = async ({ page, data: dataJson }) => {
page.setExtraHTTPHeaders({headers})
await page.authenticate({
username: proxy_user,
password: proxy_pass
});
//Randomized Delay
await delay(2000 + (Math.floor(Math.random() * 998) + 1));
const response = await page.goto(dataJson.Url);
}
//loop over inputs, and queue them into cluster
var dataJson = {
url: url
};
cluster.queue(dataJson, extract);
}
// Shutdown after everything is done
await cluster.idle();
await cluster.close();
Run Code Online (Sandbox Code Playgroud)
}
puppeteer-cluster这里的作者。该库不会主动阻止 cookie,而是利用browser.createIncognitoBrowserContext():
创建一个新的隐身浏览器上下文。这不会与其他浏览器上下文共享 cookie/缓存。
此外,文档指出“隐身浏览器上下文不会将任何浏览数据写入磁盘”(源),因此重新启动浏览器无法重用磁盘中的任何 cookie,因为没有写入数据。
关于库,这意味着在执行作业时,会创建一个新的隐身上下文,该上下文不与其他上下文共享任何数据(cookie 等)。因此,只要 Chromium 正确实现隐身浏览器上下文,作业之间就不会共享数据。
您链接的页面仅讨论browser.newPage()(在页面之间共享 cookie)而不涉及隐身上下文。
有些网站仍会阻止您,因为它们使用不同的措施来检测机器人。有模拟浏览器检测试验,以及可能举报你是机器人,如果用户代理不匹配的浏览器指纹指纹库。您可能对我的这个回答感兴趣,它提供了一些更详细的解释这些指纹是如何工作的。
您可以尝试使用类似插件puppeteer-extra附带的库stealth来帮助您解决问题。然而,这基本上是一个猫捉老鼠的游戏。指纹测试可能会更改,或者其他站点可能会使用不同的“检测”机制。总而言之,无法保证网站不会检测到您。
如果您想使用puppeteer-extra,请注意您可以将它与puppeteer-cluster(示例代码)结合使用。
| 归档时间: |
|
| 查看次数: |
3559 次 |
| 最近记录: |