Skip to content

Instantly share code, notes, and snippets.

@DonerKebab
Last active November 21, 2024 08:03
Show Gist options
  • Save DonerKebab/70e128674474645078ebf1e9a5982943 to your computer and use it in GitHub Desktop.
Save DonerKebab/70e128674474645078ebf1e9a5982943 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
// Apply the stealth plugin to avoid bot detection
puppeteer.use(StealthPlugin());
(async () => {
// Define the proxy server and a random port within the specified range
const proxyHost = 'premium.residential.proxyrack.net';
const proxyPort = Math.floor(Math.random() * (15000 - 10000 + 1)) + 10000; // Random port between 10000 and 15000
const proxyServer = `http://${proxyHost}:${proxyPort}`;
console.log(`Using proxy: ${proxyServer}`);
// Launch Puppeteer with the proxy server enabled
const browser = await puppeteer.launch({
headless: true, // Run in headless mode
args: [`--proxy-server=${proxyServer}`], // Enable proxy
});
try {
const page = await browser.newPage();
// If the proxy requires authentication, set your credentials here
const proxyUsername = 'your-username'; // Replace with your proxy username
const proxyPassword = 'your-password'; // Replace with your proxy password
if (proxyUsername && proxyPassword) {
await page.authenticate({ username: proxyUsername, password: proxyPassword });
}
// Navigate to a website
await page.goto('https://example.com', { waitUntil: 'networkidle2' });
// Extract and log page content
const content = await page.content();
console.log('Page Content:', content);
// Close the browser
await browser.close();
} catch (error) {
console.error('Error:', error);
await browser.close();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment