Linux中Chrome无界模式动态代理IP的配置(Selenium)

简介: Linux中Chrome无界模式动态代理IP的配置(Selenium)

不需要账号密码的代理设置(Windows)

from selenium import webdriver
proxy = '127.0.0.1:9743'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://' + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get('http://httpbin.org/get')

在这里我们通过 ChromeOption 来设置代理,在创建Chrom 对象的时候用 chrome_options 参数传 递即可。


使用阿布云的代理设置(Windows)

如果代理是认证代理,则设置方法相对比较麻烦,设置方法如下所示。这里需要在本地创建一个 manifest.json 置文件和 background.js 脚本来设置认证代理,运行代码 之后本地会生成一个 authProxy@http-dyn.abuyun.9020.zip 文件来保存当前配置

import base64
import string
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import zipfile
proxyHost = "http-dyn.abuyun.com"
proxyPort = "9020"
# 隧道身份信息
proxyUser = "xxxxxxxxxx"
proxyPass = "xxxxxxxxxx"
authStr = proxyUser + ":" + proxyPass
proxyAuth = "Basic " + base64.b64encode(authStr.encode('utf-8')).decode('utf-8')
def create_proxy_auth_extension(proxy_host, proxy_port,
                                proxy_username, proxy_password,
                                scheme='http', plugin_path=None):
    if plugin_path is None:
        plugin_path = r'./authProxy@http-dyn.abuyun.9020.zip'
    manifest_json = """
        {
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Abuyun Proxy",
            "permissions": [
                "proxy",
                "tabs",
                "unlimitedStorage",
                "storage",
                "<all_urls>",
                "webRequest",
                "webRequestBlocking"
            ],
            "background": {
                "scripts": ["background.js"]
            },
            "minimum_chrome_version":"22.0.0"
        }
        """
    background_js = string.Template(
        """
        var config = {
            mode: "fixed_servers",
            rules: {
                singleProxy: {
                    scheme: "${scheme}",
                    host: "${host}",
                    port: parseInt(${port})
                },
                bypassList: ["foobar.com"]
            }
          };
        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "${username}",
                    password: "${password}"
                }
            };
        }
        chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
        );
        """
    ).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme,
    )
    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)
    return plugin_path
proxy_auth_plugin_path = create_proxy_auth_extension(
    proxy_host=proxyHost,
    proxy_port=proxyPort,
    proxy_username=proxyUser,
    proxy_password=proxyPass)
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
# 通过 option.add_extension 命令安装至chrome 通过插件实现动态代理
chrome_options.add_extension(proxy_auth_plugin_path)
# 多次打开浏览器,查看代理是否设置成功
for i in range(5):
    browser = webdriver.Chrome(chrome_options=chrome_options)
    browser.get('http://httpbin.org/get')

172e0af5066d42fd~tplv-t2oaga2asx-zoom-in-crop-mark_4536_0_0_0.png

172e0ac4ffc23905~tplv-t2oaga2asx-zoom-in-crop-mark_4536_0_0_0.png

chromedriver 使用认证代理插件在无界面环境下运行

通过以上的代理设置后,会有一个普遍的问题,就是使用chromedriver添加认证代理时不能使用headless的问题。 装插件后无法直接使用无界面模式运行,可以通过虚拟现实技术间接实现pyvirtualdisplay

  1. 安装Xvfb虚拟界面工具yum install Xvfb
  2. 安装对应的python工具包pip install pyvirtualdisplay

以下为测试代码

from selenium import webdriver
from pyvirtualdisplay import Display
# 在chromedriver启动前启动一个显示器
display = Display(visible=0, size=(800, 800))
display.start()
# 使用上个例子中制作好的阿布云代理插件
plugin_path = './authProxy@http-dyn.abuyun.9020.zip'
# 添加插件及必要的配置 
option = webdriver.ChromeOptions()
option.add_argument('--no-sandbox')
option.add_extension(plugin_path)
# 测试查看效果
driver = webdriver.Chrome(chrome_options=option)
driver.get("https://httpbin.org/ip")
print(driver.page_source)
driver.quit()

172e0c905426cb5c~tplv-t2oaga2asx-zoom-in-crop-mark_4536_0_0_0.png

目录
相关文章
|
数据采集 运维 监控
构建企业级Selenium爬虫:基于隧道代理的IP管理架构
构建企业级Selenium爬虫:基于隧道代理的IP管理架构
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
1185 113
|
Ubuntu Linux 网络安全
在Linux云服务器上限制特定IP进行SSH远程连接的设置
温馨提示,修改iptables规则时要格外小心,否则可能导致无法远程访问你的服务器。最好在掌握足够技术知识和理解清楚操作含义之后再进行。另外,在已经配置了防火墙的情况下,例如ufw(Ubuntu Firewall)或firewalld,需要按照相应的防火墙的规则来设置。
884 24
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
1056 28
|
人工智能 Kubernetes Ubuntu
linux配置IP
linux配置IP
6578 1
|
Web App开发 资源调度 网络协议
Linux系统之部署IP工具箱MyIP
【10月更文挑战第5天】使用Docker部署Radicale日历和联系人应用Linux系统之部署IP工具箱MyIP
1109 1
Linux系统之部署IP工具箱MyIP
|
缓存 网络协议 Linux
Linux ip命令常用操作
Linux的 `ip`命令是一个强大且灵活的网络管理工具,能够执行从基本的网络接口配置到高级的路由和VLAN管理等多种操作。通过熟练掌握这些常用操作,用户可以更加高效地管理和配置Linux系统的网络环境。无论是在日常管理还是故障排除中,`ip`命令都是必不可少的工具。
530 2
|
负载均衡 算法 Linux
在Linux中,LVS-NAT模式的原理是什么?
在Linux中,LVS-NAT模式的原理是什么?
|
Web App开发 Linux Python
linux上安装selenium环境及测试
该文章提供了在Linux CentOS上安装Selenium环境、Chrome浏览器及Chromedriver的详细步骤,并演示了如何以无头模式进行测试。
1737 0
|
负载均衡 监控 网络协议
在Linux中,LVS-DR模式原理是什么?
在Linux中,LVS-DR模式原理是什么?