代理不使用firefox geckodriver处理python selenium

问题描述 投票:0回答:1

我有一个工作刮刀,但我无法弄清楚为什么请求不通过我设置的代理。我尝试了三种不同的方法,但没有一种方法可行。也许这是我应该检查的服务器设置上的东西?

以下是我尝试过的三个选项,没有运气。

def SetProxy1():
    options = Options()
    myProxy = '191.6.69.137:20183'
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
    })
    options.headless = True
    options.add_argument('-headless')
    driver = webdriver.Firefox(options=options, proxy=proxy)
    return driver

def SetProxy2():
    options = Options()
    options.add_argument("--headless")
    proxy = "191.6.69.137" 
    proxy_port = 20183
    proxy_profile = webdriver.FirefoxProfile()
    proxy_profile.set_preference("network.proxy.type", 1)
    proxy_profile.set_preference("network.proxy.http",proxy)
    proxy_profile.set_preference("network.proxy.http_port",int(proxy_port))
    proxy_profile.set_preference("network.proxy.https",proxy)
    proxy_profile.set_preference("network.proxy.https_port",int(proxy_port))
    proxy_profile.set_preference("network.proxy.ssl",proxy)
    proxy_profile.set_preference("network.proxy.ssl_port",int(proxy_port))  
    proxy_profile.set_preference("network.proxy.ftp",proxy)
    proxy_profile.set_preference("network.proxy.ftp_port",int(proxy_port))   
    proxy_profile.set_preference("network.proxy.socks",proxy)
    proxy_profile.set_preference("network.proxy.socks_port",int(proxy_port))  
    proxy_profile.update_preferences()
    browser = webdriver.Firefox(firefox_profile=proxy_profile, firefox_options=options)
    return browser

def SetProxy3():
    PROXY = "191.6.69.137"
    PORT = 20183
    desired_capability = webdriver.DesiredCapabilities.FIREFOX
    desired_capability['proxy']={
        "proxyType":"manual",
        "httpProxy":PROXY,
        "httpProxyPort": PORT,
        "ftpProxy":PROXY,
        "ftpProxyPort": PORT,
        "sslProxy":PROXY,
        "sslProxyPort" : PORT
    }
    options = Options()
    options.add_argument("--headless")
    driver  = webdriver.Firefox(firefox_options=options,capabilities=desired_capability)
    return driver   
python selenium proxy geckodriver
1个回答
-1
投票

这是硒的功能代理

对于Firefox

from selenium.webdriver.common.proxy import Proxy, ProxyType
def setProxy():

    myProxy = "xx.xx.xx.xx:xxxx"

    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
        'noProxy': '' # set this value as desired
        })

    driver = webdriver.Firefox(proxy=proxy)
    # Example how to use with out function :: driver.get("http://www.google.com")
    return driver

适用于Chrome

from selenium.webdriver.common.proxy import Proxy, ProxyType
def setProxy():
    prox = Proxy()
    prox.proxy_type = ProxyType.MANUAL
    prox.http_proxy = "ip_addr:port"
    prox.socks_proxy = "ip_addr:port"
    prox.ssl_proxy = "ip_addr:port"

    capabilities = webdriver.DesiredCapabilities.CHROME
    prox.add_to_capabilities(capabilities)

    driver = webdriver.Chrome(desired_capabilities=capabilities)
    return driver
© www.soinside.com 2019 - 2024. All rights reserved.