python 中的阿布云在selenium 的ip 切换

from selenium import webdriver
import string
import zipfile
import os
# 代理服务器
proxyHost = "http-dyn.abuyun.com"
proxyPort = "9020"

# 代理隧道验证信息
proxyUser = "HZOM2R924752778D"
proxyPass = "C78569FFC6E8C219"


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.getcwd()+r'/{}_{}@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"
       }
       """
    print("sadadaasda")

    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)
    print("bbbbbb")

    return plugin_path


proxy_auth_plugin_path = create_proxy_auth_extension(
    proxy_host=proxyHost,
    proxy_port=proxyPort,
    proxy_username=proxyUser,
    proxy_password=proxyPass)
print("cccccccc")

option = webdriver.ChromeOptions()
option.add_argument('-headless')

option.add_argument("--start-maximized")
option.add_extension(proxy_auth_plugin_path)
print("ddddddd")
driver = webdriver.Chrome(options=option)
print("eeeeeee")

阿布云官网参考地址 有头的测试是可以的,但是无头的好像不成功
https://www.abuyun.com/http-p...
报错如下

File "test.py", line 102, in <module>
        driver = webdriver.Chrome(options=option)
      File "/usr/lib/python3.4/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
        desired_capabilities=desired_capabilities)
      File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
        self.start_session(capabilities, browser_profile)
      File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
        self.error_handler.check_response(response)
      File "/usr/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://bbdjbmipiolfikadhebfcbojekkhbhfc/_generated_background_page.html
    from unknown error: page could not be found: chrome-extension://bbdjbmipiolfikadhebfcbojekkhbhfc/_generated_background_page.html
      (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64)
        driver.get("http://test.abuyun.com")







阅读 3.6k
1 个回答
新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题