Python爬虫 Selenium使用隧道代理

1,由于隧道代理的特点,使用selenium+隧道代理,一般用于做一次性访问达到某种目的,而不是长时间保持状态的访问,更不适用于需要跳转的情况

2,selenium+隧道代理,不可设置无头模式(不可添加 headless)

代码:

from selenium.webdriver import Chrome, ChromeOptions
import zipfile
import string


def create_proxyauth_extension(proxy_host, proxy_port,
                               proxy_username, proxy_password,
                               scheme='http', plugin_path=None):

    # 该配置不用改,可以直接用
    if plugin_path is None:
        plugin_path = 'chrome_proxyauth_plugin.zip'

    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome 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


if __name__ == '__main__':
    url = "https://www.baidu.com"

    proxyauth_plugin_path = create_proxyauth_extension(
        proxy_host="xxx.xxx.com",  # 隧道hsot
        proxy_port="",   # 隧道端口
        proxy_username="",  # 输入隧道账号
        proxy_password=""  # 输入密码
    )
    print(proxyauth_plugin_path)

    cOption = ChromeOptions()
    # cOption.add_argument('--disable-gpu')
    # cOption.add_argument('--no-sandbox')
    # cOption.add_argument('--headless')
    cOption.add_extension(proxyauth_plugin_path)

    browser = Chrome(options=cOption)
    browser.get(url)
    print(browser.page_source)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值