0

I have been trying to rotate some IPs with this piece of code. It didn't work. It still gave me my own IP. Could anyone help me check if there is anything wrong with it?

This is my code:

import random
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

ips = ["185.199.228.220:7300", "185.199.231.45:8382"]

def rand_proxy():
    proxy = random.choice(ips)
    return proxy

def myip_now(): 
   chrome_options = webdriver.ChromeOptions()
   proxy = rand_proxy()
   chrome_options.add_argument(f'--proxy-server = {proxy}')
   driver = webdriver.Chrome(options = chrome_options)
   driver.get("https://myexternalip.com/raw")
   print(proxy)
   time.sleep(10)
   driver.quit()

myip_now()

What I expected was that on https://myexternalip.com/raw controlled by my bot, I should see either 185.199.228.220:7300 or 185.199.231.45:8382.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Daisy
  • 1
  • 3

1 Answers1

0

Seems some minor issues with the blank spaces and/or single quotes. You can tweak your code block a bit removing the extra spaces and replacing the single quotes with double quotes as follows:

import random
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

ips = ["185.199.228.220:7300", "185.199.231.45:8382"]

def rand_proxy():
    proxy = random.choice(ips)
    return proxy

def myip_now(): 
   chrome_options = webdriver.ChromeOptions()
   proxy = rand_proxy()
   chrome_options.add_argument(f"--proxy-server={proxy}")
   driver = webdriver.Chrome(options = chrome_options)

   driver.get("https://myexternalip.com/raw")
   print(proxy)
   time.sleep(10)
   driver.quit()

myip_now()

Reference

You can find a couple of relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352