Selenium驱动Chrome或Firefox做自动化操作时默认走本地IP,加上代理配置就能让浏览器走代理。
Chrome配代理
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=socks5://代理IP:1080')
driver = webdriver.Chrome(options=chrome_options)带认证的代理用:--proxy-server=http://user:pass@IP:8080
Firefox配代理
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('network.proxy.type', 1)
firefox_profile.set_preference('network.proxy.socks', '代理IP')
firefox_profile.set_preference('network.proxy.socks_port', 1080)
driver = webdriver.Firefox(firefox_profile=firefox_profile)天行IP(邀请码blsj月付6元起)SOCKS5节点配合Selenium直接用。
常见问题
Selenium配代理后页面加载超时?先用浏览器手动测代理是否正常。Selenium的超时配置也要调一下。
免责声明:合规使用,遵守robots协议和法律法规。






