Tích hợp proxy với Selenium
Selenium drives a real browser, so the proxy is set at browser launch through the driver options rather than per request. Chrome takes the proxy on a --proxy-server argument but will not read credentials from it, so the reliable pattern is a small Manifest V3 extension that answers the proxy's login prompt with your Murphy username and password. The snippet below wires both together.
import json
import zipfile
from selenium import webdriver
# Residential gateway and your dashboard credentials. For an ISP proxy, use
# the IP, port, user and pass of one line from your list instead.
HOST, PORT = "gw-eu.murphyproxies.com", 8888
USER, PASS = "USERNAME", "PASSWORD"
# Chrome ignores credentials on --proxy-server, so a tiny Manifest V3
# extension answers the proxy's auth challenge instead.
manifest = {
"name": "murphy-proxy-auth",
"version": "1.0",
"manifest_version": 3,
"permissions": ["webRequest", "webRequestAuthProvider"],
"host_permissions": ["<all_urls>"],
"background": {"service_worker": "bg.js"},
}
background = f"""
chrome.webRequest.onAuthRequired.addListener(
(details, done) => done(details.isProxy
? {{authCredentials: {{username: {json.dumps(USER)}, password: {json.dumps(PASS)}}}}}
: {{}}),
{{urls: ["<all_urls>"]}},
["asyncBlocking"]
);
"""
with zipfile.ZipFile("murphy_proxy.zip", "w") as z:
z.writestr("manifest.json", json.dumps(manifest))
z.writestr("bg.js", background)
options = webdriver.ChromeOptions()
options.add_argument(f"--proxy-server=http://{HOST}:{PORT}")
options.add_extension("murphy_proxy.zip")
# Google Chrome 137+ no longer loads extensions this way: point Selenium at
# Chrome for Testing or Chromium, e.g. options.binary_location = "/path/to/chrome"
driver = webdriver.Chrome(options=options)
driver.get("https://httpbin.org/ip")
print(driver.find_element("tag name", "body").text)
driver.quit()Cài đặt trong 5 bước
- 1Install the client with pip install selenium. Selenium Manager fetches a matching chromedriver for you.
- 2Copy your Murphy host, port, username and password from the dashboard into the top of the script.
- 3Build the small Manifest V3 extension so Chrome answers the proxy authentication prompt automatically.
- 4Pass --proxy-server, load the extension with options.add_extension() and launch the driver, on Chrome for Testing or Chromium if your Google Chrome is version 137 or later.
- 5Open an IP echo page and read the body to confirm the browser exits through the proxy.
Câu hỏi về proxy cho Selenium
Mọi điều bạn cần để định tuyến Selenium qua Murphy, từ xác thực đến xoay vòng IP.
Chrome ignores the username and password embedded in the --proxy-server flag and pops a native authentication dialog that Selenium cannot dismiss. A small Manifest V3 extension with the webRequestAuthProvider permission answers that prompt automatically, which is why the snippet builds one. Google Chrome 137 and later ignore extensions loaded from the command line, which is how ChromeDriver installs them, so run the script on Chrome for Testing or Chromium.
It depends on the target. ISP proxies give a fast, stable session that suits logged-in flows and checkouts, while residential proxies give broad IP diversity that helps against strict anti-bot systems. The script is the same for both: a gw- gateway for residential, one of your dedicated IPs for ISP, so only the host, port and credentials change.
Not with a username and password. Chrome cannot authenticate to a SOCKS5 proxy, with or without an extension, so use the http:// scheme in Chrome as shown. An HTTP proxy carries HTTPS sites as well, with the same Murphy credentials.
Nhận thông tin đăng nhập cho Selenium
Chọn mạng lưới hợp với mục tiêu của bạn. Proxy ISP cho phiên nhanh và ổn định, proxy dân cư cho nguồn IP đa dạng trước các hệ thống chống bot khắt khe.