Python + Selenium + Firefox 使用代理 auth 的用户名密码授权

本文提供了使用米扑代理的详细示例,展示了如何利用Python、Selenium和Firefox配置代理访问互联网,包括无密码、白名单IP及密码授权等多种类型的代理设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

米扑代理,全球领导的代理品牌,专注代理行业近十年,提供开放、私密、独享代理,并可免费试用

米扑代理官网:https://proxy.mimvp.com

 

本文示例,是结合米扑代理的私密、独享、开放代理,专门研发的示例,

支持 http、https的无密码、白名单ip、密码授权三种类型

 

示例中,用的插件 xpi 请到米扑代理官网,或米扑官方 github 下载

本文,直接给出完整的代码,都经过严格验证通过,具体请见注释

 

Python + Firefox + 插件(closeproxy.xpi)

其中,closeproxy.xpi文件,需要Google、Bing搜下都能搜到下载地址

完整的测试代码如下:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.proxy import *
from pyvirtualdisplay import Display
from base64 import b64encode
 
 
proxy = {
    "host": "123.57.78.100",
    "port": "12345",
    "user": "username",
    "pass": "password"
}
 
profile = webdriver.FirefoxProfile()
 
# add new header
profile.add_extension("modify_headers-0.7.1.1-fx.xpi")
profile.set_preference("extensions.modify_headers.currentVersion", "0.7.1.1-fx")
profile.set_preference("modifyheaders.config.active", True)
profile.set_preference("modifyheaders.headers.count", 1)
profile.set_preference("modifyheaders.headers.action0", "Add")
profile.set_preference("modifyheaders.headers.name0", "Proxy-Switch-Ip")
profile.set_preference("modifyheaders.headers.value0", "yes")
profile.set_preference("modifyheaders.headers.enabled0", True)
 
# add proxy
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', proxy['host'])
profile.set_preference('network.proxy.http_port', int(proxy['port']))
profile.set_preference('network.proxy.no_proxies_on', 'localhost, 127.0.0.1')
#profile.set_preference("network.proxy.username", 'aaaaa')
#profile.set_preference("network.proxy.password", 'bbbbb')
 
# Proxy auto login
profile.add_extension('closeproxy.xpi')
credentials = '{user}:{pass}'.format(**proxy)
credentials = b64encode(credentials.encode('ascii')).decode('utf-8')
profile.set_preference('extensions.closeproxyauth.authtoken', credentials)
 
profile.update_preferences()
 
driver = webdriver.Firefox(profile)
driver.get("https://proxy.mimvp.com/ip.php")
print driver.page_source
 
driver.quit()

 

米扑代理出品:完整、验证、权威的示例

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Selenium + Firefox 支持 http、https
#
# 米扑代理示例:
# https://proxy.mimvp.com/demo2.php
#
# 米扑代理购买:
# https://proxy.mimvp.com
#
# mimvp.com
# 2017-01-08
 
# Python + Selenium + Firefox 设置密码时,需要使用到两个插件:
# 插件1: modify_headers-0.7.1.1-fx.xpi
# 下载地址:https://github.com/mimvp/mimvp-proxy-demo
#
# 方式2: close_proxy_authentication-1.1.xpi
# 下载地址:https://github.com/mimvp/mimvp-proxy-demo
#       
# 本示例由米扑代理原创,测试代理来自于米扑代理
# 密码授权和白名单ip设置,请见米扑代理 - 会员中心:https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip
 
 
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.proxy import *
from pyvirtualdisplay import Display
# from xvfbwrapper import Xvfb
 
import bs4, os
from base64 import b64encode
 
import sys
reload(sys)
sys.setdefaultencoding('utf8')
 
 
## webdriver + firefox (不使用代理,爬取网页)
def spider_url_firefox(url):
    browser = None
    display = None
    try:
        display = Display(visible=0, size=(800, 600))
        display.start()
        browser = webdriver.Firefox()       # 打开 FireFox 浏览器
        browser.get(url)    
        content = browser.page_source
        print("content: " + str(content))
    finally:
        if browser: browser.quit()
        if display: display.stop()
 
 
## webdriver + firefox + proxy + whiteip (无密码,或白名单ip授权)
## 米扑代理:https://proxy.mimvp.com
def spider_url_firefox_by_whiteip(url):
    browser = None
    display = None
     
    ## 白名单ip,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip
    mimvp_proxy = {
                    'ip'            : '140.143.62.84',      # ip
                    'port_https'    : 19480,                # http, https
                    'port_socks'    : 19481,                # socks5
                    'username'      : 'mimvp-user',
                    'password'      : 'mimvp-pass'
                  }
     
    try:
        display = Display(visible=0, size=(800, 600))
        display.start()
         
        profile = webdriver.FirefoxProfile()
         
        # add proxy
        profile.set_preference('network.proxy.type', 1)     # ProxyType.MANUAL = 1
        if url.startswith("http://"):
            profile.set_preference('network.proxy.http', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.http_port', mimvp_proxy['port_https'])    # 访问http网站
        elif url.startswith("https://"):
            profile.set_preference('network.proxy.ssl', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.ssl_port', mimvp_proxy['port_https'])     # 访问https网站
        else:
            profile.set_preference('network.proxy.socks', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.socks_port', mimvp_proxy['port_socks'])
            profile.set_preference('network.proxy.ftp', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.ftp_port', mimvp_proxy['port_https'])
            profile.set_preference('network.proxy.no_proxies_on', 'localhost,127.0.0.1')
         
        ## 不存在此用法,不能这么设置用户名密码 (舍弃)
#         profile.set_preference("network.proxy.username", 'mimvp-guest')
#         profile.set_preference("network.proxy.password", 'welcome2mimvp')
     
        profile.update_preferences()
         
        browser = webdriver.Firefox(profile)       # 打开 FireFox 浏览器
        browser.get(url)    
        content = browser.page_source
        print("content: " + str(content))
    finally:
        if browser: browser.quit()
        if display: display.stop()
 
 
## webdriver + firefox + proxy + https (https密码授权)
## 米扑代理:https://proxy.mimvp.com
def spider_url_firefox_by_proxy(url):
    browser = None
    display = None
     
    ## 授权密码,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip
    mimvp_proxy = {
                    'ip'            : '140.143.62.84',      # ip
                    'port_https'    : 19480,                # http, https
                    'port_socks'    : 19481,                # socks5
                    'username'      : 'mimvp-user',
                    'password'      : 'mimvp-pass'
                  }
 
    try:
        display = Display(visible=0, size=(800, 600))
        display.start()
         
        profile = webdriver.FirefoxProfile()
         
        # add new header
        profile.add_extension("modify_headers-0.7.1.1-fx.xpi")
        profile.set_preference("extensions.modify_headers.currentVersion", "0.7.1.1-fx")
        profile.set_preference("modifyheaders.config.active", True)
        profile.set_preference("modifyheaders.headers.count", 1)
        profile.set_preference("modifyheaders.headers.action0", "Add")
        profile.set_preference("modifyheaders.headers.name0", "Proxy-Switch-Ip")
        profile.set_preference("modifyheaders.headers.value0", "yes")
        profile.set_preference("modifyheaders.headers.enabled0", True)
 
        # add proxy
        profile.set_preference('network.proxy.type', 1)     # ProxyType.MANUAL = 1
        if url.startswith("http://"):
            profile.set_preference('network.proxy.http', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.http_port', mimvp_proxy['port_https'])    # 访问http网站
        elif url.startswith("https://"):
            profile.set_preference('network.proxy.ssl', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.ssl_port', mimvp_proxy['port_https'])     # 访问https网站
  
        # Proxy auto login (自动填写密码,进行代理授权)
        profile.add_extension('close_proxy_authentication-1.1.xpi')
        credentials = '{username}:{password}'.format(username=mimvp_proxy['username'], password=mimvp_proxy['password'])    # auth
        credentials = b64encode(credentials.encode('ascii')).decode('utf-8')
        profile.set_preference('extensions.closeproxyauth.authtoken', credentials)
 
        profile.update_preferences()
         
        browser = webdriver.Firefox(profile)       # 打开 FireFox 浏览器
        browser.get(url)    
        content = browser.page_source
        print("content: " + str(content))
    finally:
        if browser: browser.quit()
        if display: display.stop()
 
 
## webdriver + firefox + proxy + socks (socks密码授权)
## 米扑代理:https://proxy.mimvp.com
def spider_url_firefox_by_socks(url):
    browser = None
    display = None
     
    ## 授权密码,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip
    mimvp_proxy = {
                    'ip'            : '140.143.62.84',      # ip
                    'port_https'    : 19480,                # http, https
                    'port_socks'    : 19481,                # socks5
                    'username'      : 'mimvp-user',
                    'password'      : 'mimvp-pass'
                  }
 
    proxy_config = Proxy({
                    'proxyType'     : ProxyType.MANUAL,         # 1
                    'httpProxy'     : mimvp_proxy['ip'] + ":" + str(mimvp_proxy['port_https']),
                    'sslProxy'      : mimvp_proxy['ip'] + ":" + str(mimvp_proxy['port_https']),
                    'socksProxy'    : mimvp_proxy['ip'] + ":" + str(mimvp_proxy['port_socks']),
                    'ftpProxy'      : mimvp_proxy['ip'] + ":" + str(mimvp_proxy['port_https']),
                    'noProxy'       : 'localhost,127.0.0.1',
                    'socksUsername' : mimvp_proxy['username'],
                    'socksPassword' : mimvp_proxy['password'],
                  })
     
    try:
        display = Display(visible=0, size=(800, 600))
        display.start()
         
        profile = webdriver.FirefoxProfile()
         
        # add new header
        profile.add_extension("modify_headers-0.7.1.1-fx.xpi")
        profile.set_preference("extensions.modify_headers.currentVersion", "0.7.1.1-fx")
        profile.set_preference("modifyheaders.config.active", True)
        profile.set_preference("modifyheaders.headers.count", 1)
        profile.set_preference("modifyheaders.headers.action0", "Add")
        profile.set_preference("modifyheaders.headers.name0", "Proxy-Switch-Ip")
        profile.set_preference("modifyheaders.headers.value0", "yes")
        profile.set_preference("modifyheaders.headers.enabled0", True)
         
        # auto save auth
        profile.set_preference("signon.autologin.proxy", 'true')
        profile.set_preference("network.websocket.enabled", 'false')
        profile.set_preference('network.proxy.share_proxy_settings', 'false')
        profile.set_preference('network.automatic-ntlm-auth.allow-proxies', 'false')
        profile.set_preference('network.auth.use-sspi', 'false')
        profile.update_preferences()
         
        browser = webdriver.Firefox(proxy=proxy_config, firefox_profile=profile)       # 打开 FireFox 浏览器
        browser.get(url)    
        content = browser.page_source
        print("content: " + str(content))
    finally:
        if browser: browser.quit()
        if display: display.stop()
 
 
if __name__ == '__main__':
    url = 'https://ip.cn'
    url = 'https://mimvp.com'
    url = 'https://proxy.mimvp.com/ip.php'
     
    # 不使用代理,爬取网页,成功
    spider_url_firefox(url)
     
    # 代理无密码,或设置白名单ip,成功
    spider_url_firefox_by_whiteip(url)
     
    # http, https 密码授权,成功
    spider_url_firefox_by_proxy(url)
 
    # socks5 密码授权,失败 (仍然是本机ip请求的,不是代理ip请求)
    spider_url_firefox_by_socks(url)


### 如何在火狐浏览器中保存代理服务器的登录凭据 为了使Firefox能够自动处理代理身份验证并记住凭证,可以通过配置特定的设置来实现这一目标。通常情况下,当遇到需要认证的代理时,Firefox会弹出提示框让用户输入用户名密码。 #### 使用about:config手动配置 进入`about:config`页面可以调整一些内部参数以适应不同的需求[^2]: - `network.automatic-ntlm-auth.trusted-uris`: 将公司域名或其他信任站点加入此列表,使得NTLM协议下的代理认证能更顺畅地工作。 - `network.negotiate-auth.delegation-uris`: 类似于上述属性,用于指定允许委派Kerberos票据交换服务(TGS)请求到哪些URI上。 - `signon.rememberSignons`: 设置为`true`表示允许存储网站和服务的身份验证信息,默认即为此值;如果被设成false,则不会有任何形式的记忆功能。 然而值得注意的是,直接通过这些选项并不能完全解决HTTP(S)代理本身要求的基本/摘要式身份验证问题——因为这涉及到每次连接建立之初就要提供正确的证书资料给远程主机。 #### 利用第三方扩展或脚本自动化流程 对于那些经常切换网络环境或者频繁遭遇相同类型的代理挑战响应场景而言,安装合适的附加组件可能是更好的解决方案之一。例如,“FoxyProxy Standard”插件支持定义多个代理规则集,并且内置了简单的界面用来管理不同模式下所需的访问权限数据。 另外,在编程环境中操作浏览器实例(如Python Selenium WebDriver),则可以直接编码设定好相应的代理账号信息作为启动参数传递进去[^1]: ```python from selenium import webdriver from selenium.webdriver.firefox.options import Options as FirefoxOptions options = FirefoxOptions() proxy_host = "your.proxy.server" proxy_port = 8080 proxy_user = "username" proxy_pass = "password" firefox_profile = webdriver.FirefoxProfile() # Set the proxy settings. firefox_profile.set_preference("network.proxy.type", 1) 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)) capabilities = webdriver.DesiredCapabilities.FIREFOX capabilities['marionette'] = True # Add proxy auth extension or use other methods to handle basic/digest HTTP authentication. driver = webdriver.Firefox(firefox_profile=firefox_profile, options=options, desired_capabilities=capabilities) # Navigate somewhere... driver.get('http://example.com') ``` 这段代码展示了如何创建带有自定义配置文件的新Firefox驱动程序对象,其中包含了指向所需代理服务器及其端口的信息。不过针对基本/DIGEST-MD5这类简单保护机制来说,还需要额外手段才能让Selenium顺利越过初始阶段的身份确认环节。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值