Khi scrape dữ liệu hoặc tự động hóa với Selenium, việc dùng proxy là bắt buộc để tránh bị khóa IP.
Cách cấu hình proxy trong Selenium
Proxy HTTP/HTTPS
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--proxy-server=http://user:pass@host:port')
driver = webdriver.Chrome(options=options)`
Proxy SOCKS5
options.add_argument('--proxy-server=socks5://host:port')Cấu hình proxy có xác thực
Với proxy yêu cầu user/pass, cần dùng extension hoặc plugin:
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "host:port"
proxy.ssl_proxy = "host:port"
proxy.socks_proxy = "host:port"`
Kết hợp với rotating proxy
Để tránh bị phát hiện pattern, nên xoay IP định kỳ. Có thể dùng rotating proxy service hoặc tự quản lý pool proxy.
