如何在drissionpage中使用需认证的代理

8/1/2025 DrissionPagePython

drissionpage 的 set_proxy()方法不支持使用需认证的代理。

该方法用于设置浏览器代理。 该设置在浏览器启动时一次性设置,设置后不能修改。且不支持带账号的代理。 如果需要运行时修改代理,或使用带账号的代理,可以用插件自行实现。

如果有带认证的代理,需要自己手动实现插件。

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 = os.path.join(os.getcwd(), 'proxy_auth_extension')

    # Manifest V3 for latest Chrome versions
    manifest_json = {
        "manifest_version": 3,
        "name": "Proxy Auth Extension",
        "version": "1.0.0",
        "permissions": [
            "proxy",
            "webRequest",
            "webRequestAuthProvider"
        ],
        "host_permissions": ["<all_urls>"],
        "background": {
            "service_worker": "background.js"
        },
        "minimum_chrome_version": "88"
    }

    # Service worker for Manifest V3
    background_js = Template("""
        chrome.proxy.settings.set({
            value: {
                mode: "fixed_servers",
                rules: {
                    singleProxy: {
                        scheme: "${scheme}",
                        host: "${host}",
                        port: ${port}
                    },
                    bypassList: ["localhost", "127.0.0.1"]
                }
            },
            scope: "regular"
        });

        chrome.webRequest.onAuthRequired.addListener(
            (details) => {
                return {
                    authCredentials: {
                        username: "${username}",
                        password: "${password}"
                    }
                };
            },
            {urls: ["<all_urls>"]},
            ["blocking"]
        );
    """).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme
    )

    # Create extension directory
    os.makedirs(plugin_path, exist_ok=True)

    # Write manifest.json
    with open(os.path.join(plugin_path, "manifest.json"), "w") as f:
        json.dump(manifest_json, f, indent=2)

    # Write background.js
    with open(os.path.join(plugin_path, "background.js"), "w") as f:
        f.write(background_js)

    return plugin_path
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

然后通过options.add_extension()来使用这个插件

options = ChromiumOptions()
proxy_auth_plugin_path = create_proxy_auth_extension(
    'proxy.example.com',  # 代理服务器地址
    "8080",               # 代理服务器端口
    'user',               # 代理用户名
    'password'            # 代理密码
)
options.add_extension(path=proxy_auth_plugin_path)
page = ChromiumPage(addr_or_opts=options)
1
2
3
4
5
6
7
8
9

然后启动即可,但是大概率会发现无法安装成功插件,因为从 Chrome 137 版本开始,官方 Chrome 浏览器移除了 --load-extension 命令行标志的支持。此更改旨在提高浏览器的安全性和稳定性,防止恶意和不必要的扩展程序通过命令行加载。

会导致无法加载插件,这里需要安装一下旧版本的chrome,推荐ungoogled-chromium-windows (opens new window)

可以从这里下载编译好的https://ungoogled-software.github.io/ungoogled-chromium-binaries/releases/文件,然后在代码中使用即可。

options = ChromiumOptions()
chrome_path = os.path.join(os.getcwd(), 'ungoogled-chromium')
options.set_browser_path(chrome_path)
page = ChromiumPage(addr_or_opts=options)
1
2
3
4