在Spyder(Anaconda)上使用 selenium 进行IP轮换

vbopmzt1  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(166)

我一直在用这段代码轮换一些IP,没有用,还是给了我自己的IP,谁能帮我看看有什么问题吗?
这是我的代码:

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()

我所期望的是,在我的机器人控制的https://myexternalip.com/raw上,我应该看到185.199.228.220:7300185.199.231.45:8382

mbskvtky

mbskvtky1#

看起来空格和/或单引号有一些小问题。你可以稍微调整你的代码块,去掉多余的空格,用双引号替换单引号,如下所示:

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()

参考

您可以在以下内容中找到一些相关的详细讨论:

相关问题