子 + 2captcha代理不适用于网站

问题描述 投票:0回答:1
一起使用,但是当我尝试使用Selenium访问网站时,它会失败。

我手动验证了代理人,并且有效。 我尝试将--proxy-server添加到Chromeoptions中。 没有出现明确的错误消息,页面没有加载。

我想念什么?

当页面打开时,我得到的是我得到的(对不起,它在克罗地亚人):

there是代码的最低可重现示例:

import requests from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time # Proxy credentials USERNAME = "your_username" PASSWORD = "your_password" PROXY_DNS = "your_proxy_ip:port" def test_proxy(): """Test if the proxy is working.""" proxy_url = f"http://{USERNAME}:{PASSWORD}@{PROXY_DNS}" proxies = {"http": proxy_url, "https": proxy_url} try: response = requests.get("http://ip-api.com/json", proxies=proxies, timeout=10) print("Proxy Response:", response.json()) except requests.RequestException as e: print("Proxy failed:", e) def setup_driver(): """Setup Selenium WebDriver with proxy.""" chrome_options = webdriver.ChromeOptions() proxy = f"http://{USERNAME}:{PASSWORD}@{PROXY_DNS}" chrome_options.add_argument(f"--proxy-server={proxy}") driver = webdriver.Chrome(options=chrome_options) return driver def test_website(): """Test if Selenium can access the website.""" driver = setup_driver() driver.get("https://www.if.fi/henkiloasiakkaat/vakuutukset/autovakuutus") try: WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, "body")) ) time.sleep(10) print("Page loaded successfully.") except: print("Failed to load the page.") driver.quit() if __name__ == "__main__": test_proxy() # Check if proxy works via requests test_website() # Check if proxy works with Selenium

Error on the page 我在UpWork上看到了您的帖子,我对Python一无所知,但是我可以访问Claude 3.7,也许这会帮助您:

我看到您的硒代理配置的问题。当使用经硒的身份验证代理时,需要一些特定的方法与

requests
处理代理不同。
当前设置中缺少的是:
python selenium-webdriver proxy 2captcha
1个回答
0
投票

对于硒中身份验证的代理,使用

--proxy-server

时,您不能直接在代理URL中传递凭据。相反,您需要使用单独的方法来处理身份验证。

错误

ERR_NO_SUPPORTED_PROXIES
    通常表示Chrome无法与提供的代理配置建立连接。
  1. there是您代码的更正版本:

    import requests
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    from selenium.webdriver.common.proxy import Proxy, ProxyType
    
    # Proxy credentials
    USERNAME = "your_username"
    PASSWORD = "your_password"
    PROXY_DNS = "your_proxy_ip:port"
    
    
    def test_proxy():
        """Test if the proxy is working."""
        proxy_url = f"http://{USERNAME}:{PASSWORD}@{PROXY_DNS}"
        proxies = {"http": proxy_url, "https": proxy_url}
    
        try:
            response = requests.get("http://ip-api.com/json", proxies=proxies, timeout=10)
            print("Proxy Response:", response.json())
        except requests.RequestException as e:
            print("Proxy failed:", e)
    
    
    def setup_driver():
        """Setup Selenium WebDriver with proxy."""
        chrome_options = webdriver.ChromeOptions()
        
        # Method 1: Using Chrome extension for authentication
        # Create proxy extension
        proxy_host = PROXY_DNS.split(':')[0]
        proxy_port = PROXY_DNS.split(':')[1]
        
        manifest_json = """
        {
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Chrome Proxy",
            "permissions": [
                "proxy",
                "tabs",
                "unlimitedStorage",
                "storage",
                "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: []
            }
        };
    
        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']
        );
        """ % (proxy_host, proxy_port, USERNAME, PASSWORD)
        
        import os
        import zipfile
        import tempfile
        
        # Create a temporary directory
        plugin_dir = tempfile.mkdtemp()
        
        # Write the extension files
        with open(os.path.join(plugin_dir, 'manifest.json'), 'w') as f:
            f.write(manifest_json)
        with open(os.path.join(plugin_dir, 'background.js'), 'w') as f:
            f.write(background_js)
        
        # Create a zip file
        plugin_path = os.path.join(plugin_dir, 'proxy_auth_plugin.zip')
        with zipfile.ZipFile(plugin_path, 'w') as zp:
            zp.write(os.path.join(plugin_dir, 'manifest.json'), 'manifest.json')
            zp.write(os.path.join(plugin_dir, 'background.js'), 'background.js')
        
        # Add the extension to Chrome
        chrome_options.add_extension(plugin_path)
        
        # Additional recommended options
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        
        # Create and return the driver
        driver = webdriver.Chrome(options=chrome_options)
        return driver
    
    
    def test_website():
        """Test if Selenium can access the website."""
        driver = setup_driver()
        
        try:
            # First verify our IP through the proxy
            driver.get("https://api.ipify.org")
            print(f"Current IP via proxy: {driver.find_element(By.TAG_NAME, 'pre').text}")
            
            # Then try the actual website
            driver.get("https://www.if.fi/henkiloasiakkaat/vakuutukset/autovakuutus")
            
            WebDriverWait(driver, 20).until(
                EC.presence_of_element_located((By.TAG_NAME, "body"))
            )
            print("Page loaded successfully.")
            print(f"Current URL: {driver.current_url}")
            
        except Exception as e:
            print(f"Failed to load the page. Error: {e}")
        finally:
            driver.quit()
    
    
    if __name__ == "__main__":
        test_proxy()  # Check if proxy works via requests
        test_website()  # Check if proxy works with Selenium
    

  2. 键的变化和解释:
  3. 染色扩展方法:对于认证的代理,创建一个镀铬扩展以处理身份验证是最可靠的方法之一。这就是我在
  4. setup_driver()
函数中实现的目标。

IP验证

:我添加了一张检查,以验证您在尝试访问目标网站之前与代理使用的IP地址。

  1. Error处理:我已经改进了错误处理以显示更具体的错误消息。

    TimeOut
  2. :将等待时间提高到20秒以适应可能较慢的代理连接。
  3. 替代方法如果这不起作用:

    您可以尝试使用PAC(代理自动配置)文件:
  4. chrome_options.add_argument(f"--proxy-pac-url=data:text/javascript,{{'FindProxyForURL': function(url, host) {{ return 'PROXY {PROXY_DNS}'; }}}}")

    或使用Selenium的内置代理配置(尽管这也无法处理身份验证):
  5. proxy = Proxy() proxy.proxy_type = ProxyType.MANUAL proxy.http_proxy = PROXY_DNS proxy.ssl_proxy = PROXY_DNS capabilities = webdriver.DesiredCapabilities.CHROME proxy.add_to_capabilities(capabilities) driver = webdriver.Chrome(desired_capabilities=capabilities, options=chrome_options)

© www.soinside.com 2019 - 2025. All rights reserved.