HTTP隧道 (动态版)
Selenium 接入指南

隧道验证方式

地址:http-dyn.abuyun.com,端口:9020

隧道验证方式

HTTP隧道拥有两种授权模式:

  • 请求头(默认)

通过用户名/密码的形式进行身份认证,该认证信息最终会转换为『Proxy-Authorization』协议头跟随请求一起发出。

为便于部分语言进行接入,平台亦支持通过『Authorization』协议头进行隧道身份验证。

  • 绑定IP(暂未启用)

只须绑定用户发起请求的服务器IP即可。

一条代理隧道只能绑定一个IP,同一IP可以分别绑定到专业版、动态版、经典版代理隧道各一条。

              
    # -*-*-
    # 感谢骚男 『专注爬虫二十年 (QQ: 601245227)』 提供的源代码
    # -*-*-

    import org.json.JSONException;
    import org.json.JSONObject;
    import org.openqa.selenium.Platform;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;

    import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
    import com.gargoylesoftware.htmlunit.WebClient;

    public class FirefoxDriverProxyDemo
    {
        // 代理隧道验证信息
        final static String proxyUser = "H01234567890123D";
        final static String proxyPass = "0123456789012345";

        // 代理服务器
        final static String proxyHost = "http-dyn.abuyun.com";
        final static int proxyPort = 9020;

        final static String firefoxBin = "D:/Program Files/Mozilla Firefox/firefox.exe";

        public static void main(String[] args) throws JSONException
        {
            System.setProperty("webdriver.firefox.bin", firefoxBin);

            FirefoxProfile profile = new FirefoxProfile();
            // 使用代理
            profile.setPreference("network.proxy.type", 1);
            // 代理服务器配置
            profile.setPreference("network.proxy.http", proxyHost);
            profile.setPreference("network.proxy.http_port", proxyPort);

            profile.setPreference("network.proxy.ssl", proxyHost);
            profile.setPreference("network.proxy.ssl_port", proxyPort);

            profile.setPreference("username", proxyUser);
            profile.setPreference("password", proxyPass);

            // 所有协议公用一种代理配置,如果单独配置,这项设置为false
            profile.setPreference("network.proxy.share_proxy_settings", true);

            // 对于localhost的不用代理,这里必须要配置,否则无法和webdriver通讯
            profile.setPreference("network.proxy.no_proxies_on", "localhost");

            // 以代理方式启动firefox
            FirefoxDriver driver = new FirefoxDriver(profile);
        }
    }              
            
              
    # -*-*-
    # 感谢骚男 『专注爬虫二十年 (QQ: 601245227)』 提供的源代码
    # -*-*-

    import org.json.JSONException;
    import org.json.JSONObject;
    import org.openqa.selenium.Platform;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;

    import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
    import com.gargoylesoftware.htmlunit.WebClient;

    public class HtmlUnitDriverProxyDemo
    {
        // 代理隧道验证信息
        final static String proxyUser = "H01234567890123D";
        final static String proxyPass = "0123456789012345";

        // 代理服务器
        final static String proxyServer = "http-dyn.abuyun.com:9020";

        public static void main(String[] args) throws JSONException
        {
            HtmlUnitDriver driver = getHtmlUnitDriver();

            driver.get("https://test.abuyun.com");

            String title = driver.getTitle();
            System.out.println(title);
        }

        public static HtmlUnitDriver getHtmlUnitDriver()
        {
            HtmlUnitDriver driver = null;

            Proxy proxy = new Proxy();
            // 设置代理服务器地址
            proxy.setHttpProxy(proxyServer);

            DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
            capabilities.setCapability(CapabilityType.PROXY, proxy);
            capabilities.setJavascriptEnabled(true);
            capabilities.setPlatform(Platform.WIN8_1);

            driver = new HtmlUnitDriver(capabilities) {
                @Override
                protected WebClient modifyWebClient(WebClient client) {
                    DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
                    creds.addCredentials(proxyUser, proxyPass);
                    client.setCredentialsProvider(creds);
                    return client;
                }
            };

            driver.setJavascriptEnabled(true);

            return driver;
        }
    }              
            
              
    # -*-*-
    # 感谢骚男 『jiehuhu (微信公众号: 杰呼呼)』 提供的源代码
    # -*-*-

    import java.util.ArrayList;
    import java.util.concurrent.TimeUnit;

    import org.apache.commons.lang.StringUtils;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriverService;
    import org.openqa.selenium.remote.DesiredCapabilities;

    public class PhantomJSDriverDome
    {
        public static String proxyHost = "http-dyn.abuyun.com";
        public static int proxyPort = 9020;

        public static String proxyUser = "H01234567890123D";
        public static String proxyPass = "0123456789012345";

        // 本机phantomjs的存储路径
        public static String PHANTOMJS_BINARY_PATH = "E:/phantomjs-2.1.1-windows/bin/phantomjs.exe";

        public static WebDriver getPhantomjsDriver()
        {
            return getPhantomjsDriver("", 0);
        }

        public static WebDriver getPhantomjsDriver(String proxyIp, int proxyPort)
        {
            System.setProperty("phantomjs.binary.path", PHANTOMJS_BINARY_PATH);

            WebDriver driver = null;
            DesiredCapabilities cap = DesiredCapabilities.phantomjs();

            if ((StringUtils.isNotBlank(proxyIp)) && (proxyPort != 0))
            {
                String proxyIpAndPort = proxyIp + ":" + proxyPort;

                Proxy proxy = new Proxy();
                proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);

                cap.setCapability("avoidProxy", true);
                cap.setCapability("onlyProxySeleniumTraffic", true);

                System.setProperty("http.nonProxyHosts", "localhost");

                cap.setCapability("proxy", proxy);
            }

            driver = new PhantomJSDriver(cap);

            driver.manage().timeouts().implicitlyWait(5L, TimeUnit.SECONDS);
            driver.manage().window().maximize();

            return driver;
        }

        public static WebDriver getPhantomjsDriver(String address, String username, String password)
        {
            System.setProperty("phantomjs.binary.path", PHANTOMJS_BINARY_PATH);

            WebDriver driver = null;
            ArrayList cliArgsCap = new ArrayList();

            cliArgsCap.add("--proxy="+address);
            cliArgsCap.add("--proxy-auth=" + username + ":" + password);
            cliArgsCap.add("--proxy-type=http");

            DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();

            capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

            driver = new PhantomJSDriver(capabilities);
            driver.manage().timeouts().implicitlyWait(5L, TimeUnit.SECONDS);
            driver.manage().window().maximize();

            return driver;
        }

        public static void main(String[] args)
        {
            WebDriver driver = getPhantomjsDriver(proxyHost, proxyUser, proxyPass);

            driver.get("http://test.abuyun.com");

            System.out.println(driver.getPageSource());
        }
    }
              
            
              
    from selenium import webdriver

    # 代理服务器
    proxyHost = "http-dyn.abuyun.com"
    proxyPort = "9020"

    # 代理隧道验证信息
    proxyUser = "H01234567890123D"
    proxyPass = "0123456789012345"

    service_args = [
        "--proxy-type=http",
        "--proxy=%(host)s:%(port)s" % {
            "host" : proxyHost,
            "port" : proxyPort,
        },
        "--proxy-auth=%(user)s:%(pass)s" % {
            "user" : proxyUser,
            "pass" : proxyPass,
        },
    ]

    # 要访问的目标页面
    targetUrl = "http://test.abuyun.com"

    phantomjs_path = r"./phantomjs"

    driver = webdriver.PhantomJS(executable_path=phantomjs_path, service_args=service_args)
    driver.get(targetUrl)

    print driver.title
    print driver.page_source.encode("utf-8")

    driver.quit()              
            
              
    # -*-*-
    # 感谢骚男 『magic (QQ: 2191943283)』 提供的源代码
    # 详细参考:[https://www.jianshu.com/p/6b7f31a78f33](https://www.jianshu.com/p/6b7f31a78f33)
    # -*-*-

    from selenium import webdriver
    import string
    import zipfile

    # 代理服务器
    proxyHost = "http-dyn.abuyun.com"
    proxyPort = "9020"

    # 代理隧道验证信息
    proxyUser = "H01234567890123D"
    proxyPass = "0123456789012345"

    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'D:/{}_{}@http-dyn.abuyun.com_9020.zip'.format(proxy_username, proxy_password)

        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)

    option = webdriver.ChromeOptions()

    option.add_argument("--start-maximized")
    option.add_extension(proxy_auth_plugin_path)

    driver = webdriver.Chrome(chrome_options=option)

    driver.get("http://test.abuyun.com")
              
            
体验服务
现在开始,体验阿布云大数据服务
计算、网络、大数据、人工智能,阿布云助您飞跃发展