繁体   English   中英

如何让 puppeteer 等待来自 Cloudflare 浏览器检查的页面重定向?

[英]How to make puppeteer wait for page redirect from Cloudflare browser check?

我正在抓取一个网站,在提交表单后我被重定向到这个 -

Checking your browser before accessing <Website Name>.
This process is automatic. Your browser will redirect to your requested content shortly.

Please allow up to 5 seconds…

DDoS protection by Cloudflare
Ray ID: <Some ID>

现在,通常当我自己从“真正的 web 浏览器”手动提交该表单时,在浏览器检查内容出现后,我几乎立即被重定向到主要内容。 但在木偶戏中,它没有。

我曾尝试使用page.waitForNavigation()但无法正常工作。 有什么办法可以真正通过这个检查过程吗? 或者 puppeteer 只是被阻止了?

提前致谢!

您可以使用puppeteer-extra来添加不同的插件,例如StealthPluginAdblockerPlugin 看看他们的文档。

您可以在 web 页面上等待特定元素。 例如 header 什么的。

使用await page.waitForElement(selector);

Cloudflare 的 WAF 会在您尝试连接到网站时检查一些内容,例如请求的标头和 TLS 握手字段。 这些被收集在有效负载中并发送到 Cloudflare 的服务器。 不一致将使您被阻止或重定向。

绕过这种保护的更简单方法是使用puppeteer-extra ,它允许您在 Puppeteer 中使用插件。 然后您可以使用puppeteer-extra-plugin-stealth ,它实现了许多功能来隐藏客户端并被 WAF 忽略。

如果问题出在您的 IP(或地理位置)上,您可以查看代理链package。

这是使用 puppeteer-extra-plugin-stealth 和 proxy-chain 的最小工作示例(使用 HTTP 代理):

const puppeteer = require('puppeteer-extra')
const randUserAgent = require('rand-user-agent');
const proxyChain = require('proxy-chain');
const StealthPlugin = require('puppeteer-extra-plugin-stealth')();

puppeteer.use(StealthPlugin)

const agent = randUserAgent("desktop");
const proxyUrl = 'http://userid:pw@ip:port'
const proxy = async () => {
  return proxyChain.anonymizeProxy(proxyUrl);
}

puppeteer.launch({ headless: true,
  args: [
    '--proxy-server='+proxy
  ]
}).then(async browser => {
  const page = await browser.newPage()
  
  await page.setUserAgent(agent);
  await page.goto('https://bot.sannysoft.com', { waitUntil: 'networkidle2' })
  await page.waitForTimeout(5000) 
  await page.screenshot({ path: 'test.png', fullPage: true })
  await browser.close()
})

您可以从此处了解有关 Cloudflare 使用的方法的更多信息: Cloudflare 的机器人保护

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM