Cloudflare Challenge is a widely used anti-bot mechanism designed to protect website resources from malicious traffic. One of its core features is the Cloudflare 5s Challenge, which effectively blocks automated requests through dynamic verification.
The Cloudflare 5s Challenge is an intelligent verification mechanism. When a user accesses a website protected by Cloudflare, the system forces them to wait for 5 seconds. During this period, Cloudflare performs a series of checks on the user's browser environment, including but not limited to:
For web scraping developers, the Cloudflare 5s Challenge presents the following challenges:
By combining Puppeteer with Nstbrowser's Browserless cloud browser service, you can simulate the behavior of real users' browsers to bypass the Cloudflare 5s Challenge and continue executing your web scraping tasks.
Browserless is a high-performance headless cloud browser product provided by Nstbrowser. It is designed for automation tasks, offering powerful remote control capabilities via API and WebSocket.
With Browserless, developers can easily bypass complex anti-bot mechanisms (such as the Cloudflare Challenge) and focus on implementing their business logic without worrying about technical barriers.
npm install puppeteer-core
When writing your business logic with Puppeteer, you no longer need to worry about being blocked by the Cloudflare 5s Challenge. Nstbrowser's Browserless cloud service will automatically handle the verification for you, allowing you to focus solely on your code.
Below is a complete example script:
import puppeteer from 'puppeteer-core';
const API_KEY = "your api key"; // required
const HOST = 'wss://less.nstbrowser.io';
const config = {
proxy: 'your proxy',
headless: true,
};
const query = new URLSearchParams({
"x-api-key": API_KEY, // required
"config": JSON.stringify(config),
});
const browserWSEndpoint = `${HOST}/connect?${query.toString()}`;
(async () => {
const browser = await puppeteer.connect({
browserWSEndpoint: browserWSEndpoint,
defaultViewport: null,
});
const page = await browser.newPage();
try {
await page.goto('https://www.scrapingcourse.com/cloudflare-challenge', {waitUntil: 'domcontentloaded'});
// TODO: Add your business logic here
await page.waitForSelector('#challenge-info', {visible: true});
await page.screenshot({path: 'challenge-solved.png', fullPage: true});
} catch (e) {
console.error(e);
} finally {
await browser.close();
}
})();
You can also directly test the actual code execution in the Playground feature under the Browserless menu of the Nstbrowser client.
By following this guide, developers can easily bypass the Cloudflare 5s Challenge while leveraging Nstbrowser's Browserless cloud service to significantly enhance the efficiency and stability of their web scraping projects.