PhantomJS(Python+Selenium)自定义User Agent及代理认证问题求助
解决方案:自定义UA + 带认证代理(Chrome/Firefox,无需修改系统代理)
我来帮你搞定这个头疼的问题!你需要同时实现自定义User Agent和带认证的代理连接(还不能改系统代理),Chrome和Firefox都有靠谱的解决方案,之前你踩的坑我都懂,下面给你详细的步骤和代码:
Chrome 解决方案
你之前说Chrome没法配置代理,其实是没找对正确的姿势——Chrome确实不支持直接在Options里写代理认证,但我们可以通过注入JavaScript自动添加认证头的方式绕过这个限制,同时自定义UA直接通过参数就能生效。
实现步骤:
- 通过
ChromeOptions设置自定义User Agent和代理服务器(只填host:port,不带认证) - 启动浏览器后,注入脚本在请求头里添加Basic认证,避免弹出代理登录框
- 验证UA和代理是否生效
完整代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options # 替换成你的代理和UA信息 PROXY_HOST = "你的代理地址" PROXY_PORT = "你的代理端口" PROXY_USER = "代理用户名" PROXY_PASS = "代理密码" CUSTOM_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" # 初始化Chrome配置 chrome_options = Options() # 设置自定义User Agent chrome_options.add_argument(f"user-agent={CUSTOM_UA}") # 设置代理服务器(仅host:port,不带认证) chrome_options.add_argument(f"--proxy-server=http://{PROXY_HOST}:{PROXY_PORT}") # 启动Chrome浏览器 driver = webdriver.Chrome(options=chrome_options) # 注入脚本自动添加代理认证头 driver.execute_script(""" const username = arguments[0]; const password = arguments[1]; // 用XMLHttpRequest发送带认证的请求,让Chrome记住认证信息 const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://httpbin.org/ip', true); xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ':' + password)); xhr.send(); """, PROXY_USER, PROXY_PASS) # 验证效果 print("当前User Agent:", driver.execute_script("return navigator.userAgent")) driver.get("https://httpbin.org/ip") print("代理IP验证结果:\n", driver.page_source) # 关闭浏览器 driver.quit()
Firefox 解决方案
Firefox的代理配置需要通过FirefoxProfile来设置,之前你可能没正确配置代理的认证参数。下面的方法可以直接在Profile里设置代理、认证信息和自定义UA,完全不需要修改系统代理。
实现步骤:
- 创建
FirefoxProfile,手动配置代理类型、服务器地址和端口 - 在Profile里设置代理的用户名和密码
- 覆盖默认UA为自定义值
- 启动Firefox并验证
完整代码:
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.firefox_profile import FirefoxProfile import base64 # 替换成你的代理和UA信息 PROXY_HOST = "你的代理地址" PROXY_PORT = "你的代理端口" PROXY_USER = "代理用户名" PROXY_PASS = "代理密码" CUSTOM_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:118.0) Gecko/20100101 Firefox/118.0" # 创建Firefox配置文件 firefox_profile = FirefoxProfile() # 配置代理(手动模式) firefox_profile.set_preference("network.proxy.type", 1) # 设置HTTP、HTTPS、SOCKS代理 firefox_profile.set_preference("network.proxy.http", PROXY_HOST) firefox_profile.set_preference("network.proxy.http_port", int(PROXY_PORT)) firefox_profile.set_preference("network.proxy.ssl", PROXY_HOST) firefox_profile.set_preference("network.proxy.ssl_port", int(PROXY_PORT)) firefox_profile.set_preference("network.proxy.socks", PROXY_HOST) firefox_profile.set_preference("network.proxy.socks_port", int(PROXY_PORT)) # 所有请求都走代理,不例外 firefox_profile.set_preference("network.proxy.no_proxies_on", "") # 设置代理认证 firefox_profile.set_preference("network.proxy.http_proxy_username", PROXY_USER) firefox_profile.set_preference("network.proxy.http_proxy_password", PROXY_PASS) firefox_profile.set_preference("network.proxy.socks_username", PROXY_USER) firefox_profile.set_preference("network.proxy.socks_password", PROXY_PASS) # 禁止浏览器拦截代理认证(避免弹窗) firefox_profile.set_preference("network.http.phishy-userpass-length", 255) # 设置自定义User Agent firefox_profile.set_preference("general.useragent.override", CUSTOM_UA) # 初始化Firefox选项 firefox_options = Options() firefox_options.profile = firefox_profile # 启动Firefox浏览器 driver = webdriver.Firefox(options=firefox_options) # 验证效果 print("当前User Agent:", driver.execute_script("return navigator.userAgent")) driver.get("https://httpbin.org/ip") print("代理IP验证结果:\n", driver.page_source) # 关闭浏览器 driver.quit()
关于PhantomJS的说明
PhantomJS已经停止维护多年,很多现代网站的JS特性它都不支持,而且你遇到的UA不生效问题,是因为它的navigator.userAgent被内部硬编码覆盖了,即使在capabilities里设置也没用,所以强烈建议放弃PhantomJS,切换到Chrome或Firefox。
内容的提问来源于stack exchange,提问作者Xonshiz
相关产品推荐
相关产品推荐

