Advertisement
dereksir

Untitled

Feb 28th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // import the required libraries
  2. const { Builder, By } = require('selenium-webdriver');
  3. const firefox = require('selenium-webdriver/firefox');
  4. const proxyChain = require('proxy-chain');
  5. const proxy = require('selenium-webdriver/proxy');
  6.  
  7. async function scraper() {
  8.  
  9.     // define your proxy details
  10.     const proxyUsername = 'username';
  11.     const proxyPassword = 'password';
  12.     const ipHost = '167.99.174.59';
  13.     const port = '80';
  14.     const proxyUrl = `http://${proxyUsername}:${proxyPassword}@${ipHost}:${port}`;
  15.  
  16.     // anonymize proxyUrl
  17.     const anonymizedProxy = await proxyChain.anonymizeProxy(proxyUrl);
  18.  
  19.     // parse anonymized proxy URL
  20.     const parsedUrl = new URL(anonymizedProxy);
  21.  
  22.     // extract the host and port
  23.     const proxyHost = parsedUrl.hostname;
  24.     const proxyPort = parsedUrl.port;
  25.  
  26.     // construct the new proxy string
  27.     const newProxyString = `${proxyHost}:${proxyPort}`;
  28.    
  29.     // set the browser options
  30.     const options = new firefox.Options().addArguments('--headless');
  31.  
  32.     // initialize the webdriver
  33.     const driver = new Builder()
  34.         .forBrowser('firefox')
  35.         // add the proxy to the webdriver
  36.         .setProxy(proxy.manual({
  37.             http: newProxyString,
  38.             https: newProxyString,
  39.         }))
  40.         .setFirefoxOptions(options)
  41.         .build();
  42.    
  43.     try {
  44.        
  45.         // navigate to target website
  46.         await driver.get('https://httpbin.io/ip');
  47.  
  48.         await driver.sleep(8000);
  49.  
  50.         // get the page text content
  51.         const pageText = await driver.findElement(By.css('body')).getText();
  52.         console.log(pageText);
  53.     } catch (error) {
  54.         console.error('An error occurred:', error);
  55.     } finally {
  56.         await driver.quit();
  57.  
  58.         // clean up, forcibly close all pending connections
  59.         await proxyChain.closeAnonymizedProxy(newProxyString, true);
  60.     }
  61. }
  62.  
  63. scraper();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement