代理使用

代理使用

以插件的形式使用

#!/usr/bin/env python3  
# -*- coding: UTF-8 -*-  
"""  
@Project :all_daily_tasks_code @File    :use_proxy.py  
@Author  :木子  
@Date    :2024/2/22 14:47 """  

from DrissionPage import ChromiumPage, ChromiumOptions  
import string  
import os  
import time  


def create_proxyauth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme='http',  
                               plugin_folder=None):  
    if plugin_folder is None:  
        plugin_folder = 'kdl_Chromium_Proxy'  # 插件文件夹名称  
    if not os.path.exists(plugin_folder):  
        os.makedirs(plugin_folder)  
    manifest_json = """  
        {            "version": "1.0.0",            "manifest_version": 2,            "name": "kdl_Chromium_Proxy",            "permissions": [                "proxy",                "tabs",                "unlimitedStorage",                "storage",                "<all_urls>",                "webRequest",                "webRequestBlocking",                "browsingData"            ],            "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: []            }        };  
        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 open(os.path.join(plugin_folder, "manifest.json"), "w") as manifest_file:  
        manifest_file.write(manifest_json)  
    with open(os.path.join(plugin_folder, "background.js"), "w") as background_file:  
        background_file.write(background_js)  
    return plugin_folder  


tunnelhost = 'tunnelhost'  # 隧道域名  
tunnelport = 'tunnelport'  # 端口号  

username = 'username'  # 代理用户名  
password = 'password'  # 代理密码  

# 要访问的目标网页  
url = 'https://dev.kdlapi.com/testproxy'  
# 以插件的形式加载隧道代理  
proxyauth_plugin_folder = create_proxyauth_extension(  
    proxy_host=tunnelhost,  
    proxy_port=tunnelport,  
    proxy_username=username,  
    proxy_password=password  
)  

co = ChromiumOptions()  
current_directory = os.path.dirname(os.path.abspath(__file__))  
co.add_extension(os.path.join(current_directory, 'kdl_Chromium_Proxy'))  

page = ChromiumPage(co)  
page.get(url)  

# 获取页面内容  
print(page.html)  

# 等待3秒后关闭页面  
time.sleep(3)  
page.quit()
LICENSED UNDER CC BY-NC-SA 4.0