【问题标题】:How to enable proxy with username and password using undetected_chromium?如何使用 undetected_chromium 启用带有用户名和密码的代理?
【发布时间】:2022-06-15 01:19:11
【问题描述】:

我正在使用 undetected_chromedriver 但我无法让代理工作 我有使用用户名和密码的代理,但我无法让它工作 我的 chrome 会话开始了,但它显示当前的 Ip 没有改变它

我的代码:


import undetected_chromedriver.v2 as uc
import os
import zipfile
import time

host = '91.201.40.130'
port = 12115
username = 'alos081100RnC5'
password = 'YRhSahmNYX'


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 = """
var config = {
        mode: "fixed_servers",
        rules: {
          singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
          },
          bypassList: ["localhost"]
        }
      };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%s",
            password: "%s"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);
""" % (host, port , username, password)


def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    chrome_options = uc.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    driver = uc.Chrome(options=chrome_options)
    return driver 

def main():
    driver = get_chromedriver(use_proxy=True)
    driver.get("https://whatismyipaddress.com")
    

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python google-chrome proxy helper undetected-chromedriver


    【解决方案1】:

    您需要映射您的代理。然后迭代它。

    【讨论】:

    • 相同的代码适用于 selenium 但不适用于 undetected_chromedriver .IDK 如何映射它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2019-07-30
    • 2022-06-15
    • 2014-01-26
    相关资源
    最近更新 更多