selenium使用代理IP
设置代理IP:
有时候频繁爬取一些网页,服务器发现你是爬虫后会封掉你的ip地址,这时候我们可以更改代理ip。更改代理ip,不同的浏览器有不同的实现方式。这里以Chrome浏览器为例:
以下是某代理IP网站搜索到的代理IP:
以下是代码:
from selenium import webdriver
driver_path = r"D:\application\chromedriver_win32\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server-http://223.223.187.195:80") # 设定代理ip地址
driver = webdriver.Chrome(executable_path=driver_path, chrome_opeions=options)
driver.get("http://httpbin.org/ip") # 注意这里http://httpbin.org/ip 是返回IP的网址