因此,我使用 TOR 来更改我的 webdriver 的代理和 IP 地址。这是代码。已安装所有依赖项(包括 Geckodriver 和最新版本的 Firefox)。
from stem import Signal
from stem.control import Controller
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
def switchIP():
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
def my_proxy(PROXY_HOST,PROXY_PORT):
fp = webdriver.FirefoxProfile()
fp.set_preference("network.proxy.type", 1)
fp.set_preference("network.proxy.socks",PROXY_HOST)
fp.set_preference("network.proxy.socks_port",int(PROXY_PORT))
fp.update_preferences()
options = Options()
options.headless = True
return webdriver.Firefox(options=options, firefox_profile=fp)
for x in range(10):
proxy = my_proxy("127.0.0.1", 9050)
proxy.get("https://whatsmyip.com/")
html = proxy.page_source
soup = BeautifulSoup(html, 'lxml')
print(soup.find("span", {"id": "ipv4"}))
print(soup.find("span", {"id": "ipv6"}))
switchIP()
感谢您的帮助,阿拉夫。