首页 > 编程语言 >Python + Selenium + Firefox 使用代理 auth 的用户名密码授权

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

时间:2023-10-20 13:59:57浏览次数:31  
标签:profile set Firefox Python Selenium proxy https mimvp preference

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)

  

 

标签:profile,set,Firefox,Python,Selenium,proxy,https,mimvp,preference
From: https://www.cnblogs.com/pythonClub/p/17776871.html

相关文章

  • selenium无头浏览器,禁用图片,禁用js,切换UA,反爬
    fromseleniumimportwebdriverfromfake_useragentimportUserAgentua=UserAgent().randomoptions=webdriver.ChromeOptions()options.add_argument('--no-sandbox')#停用沙箱options.add_argument('--disable-gpu')#禁用GPU实现加速options.ad......
  • 谈谈selenium中的cookie操作
    实例演示以登录网易云音乐为例分两步走获取cookiesfromseleniumimportwebdriverfrompprintimportpprintdriver=webdriver.Chrome()driver.maximize_window()driver.get('https://music.163.com/')driver.delete_all_cookies()#此处只是演示api,可以不写的inpu......
  • 关于Python的打包与编译
    1、nuitka编译成一个so文件nuitka3--module--include-module=target_file_or_dirtarget_file_or_dir2、compileall编译成pycpython3-mcompileall-b<dir>#删除相关的py文件find<dir>-name'*.py'-typef-print-execrm{}\;3、bdist_wheel打包whl文......
  • Python3+selenium3+Firefox 设置浏览器headless模式运行+下载文件
    设置Firefoxheadless模式   defsetUp(self):#Firefoxheadless模式运行options=webdriver.FirefoxOptions()options.add_argument('-headless')self.driver=webdriver.Firefox(options=options)self.driver.implicitly_wait(30)......
  • Windows Python 访问达梦数据库(环境配置)
    WindowsPython访问达梦数据库(环境配置) 一、前提条件本篇博客以访问本地达梦数据库(DM8)为基础进行演示。(前提:本地已经安装了DM8数据库!)关于Windows安装达梦数据库,请参考博客:Windows安装达梦数据库关于Docker安装达梦数据库,请参考博客:Docker安装达梦数据库关于JD......
  • 【Python&RS】基于Python批量镶嵌拼接遥感影像/栅格数据
    ​    我之前分享过【Python&RS】基于GDAL镶嵌拼接遥感影像,但是没有加入批量处理的代码。最近正好有这个需求,所以就对原来的代码进行了优化加入了批量拼接的代码。现在只需输入一个文件夹即可将其中的影像全部镶嵌起来。 一、导入GDAL库fromosgeoimportgdal二......
  • selenium设置火狐浏览器为headless(无头模式)
    selenium已经停止了对PhantomJS的支持,只能调用Firefox或者Chrome浏览的无头模式(即没有浏览器界面)。使用步骤:安装Firefox浏览器firefox历年版本安装包的官方镜像地址:https://download-installer.cdn.mozilla.net/pub/firefox/releases/安装geckodriver驱动(1)下载geckodriver火狐所......
  • 谈谈selenium4.0中的相对定位
    相对定位历史2021-10-13发布的selenium4.0开始引入,selenium3.X是没有的implementrelativelocatorforfind_element(#9902)4.10维护了下Improvenearrelativelocatorbehavior(#11290)其他都是文档、异常信息方面的处理实例演示D:\selenium\demo\relative......
  • Python猴子补丁
    Python猴子补丁介绍猴子补丁是一种替换方法的方式。因为python是动态语言,所以我们在方法执行之前,可以将方法替换,以达到我们期望的结果。需要理解的是,python的方法在加上括号之前,代表的的只是方法的内存,可以被当做一个变量进行传递。使用#示例classTest:  a=1 ......
  • [920] Copy the font style from one cell in a table of a Word document to another
    TocopythefontstylefromonecellinatableofaWorddocumenttoanothercellusingPythonandthepython-docxlibrary,youcanaccessthefontpropertiesofthesourcecellandapplythemtothetargetcell.Here'showyoucandoit:First,ma......