Selenium Python 更改 IP

Jay*_*ean 6 python ip selenium web-scraping

我正在使用 Selenium for Python 编写一个网络爬虫。抓取工具每小时多次访问相同的站点,因此我希望找到一种方法,每隔几次搜索就更改我的 IP。对此的最佳策略是什么(我正在使用 Firefox)?是否有任何预先编写的代码/我可以切换的 IP 地址 csv?我对屏蔽 IP、代理等完全陌生,所以请放轻松!

小智 2

尝试使用代理。有免费选项(不太可靠)或付费服务。

from selenium import webdriver
    
def change_proxy(proxy,port):
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.http", proxy)
    profile.set_preference("network.proxy.http_port", port)
    profile.set_preference("network.proxy.ssl", proxy)
    profile.set_preference("network.proxy.ssl_port", port)
    driver = webdriver.Firefox(profile)
    return driver
Run Code Online (Sandbox Code Playgroud)