使用Firefox Geckodriver代理无法在python硒上工作

时间:2019-03-13 13:11:00

标签: python selenium proxy geckodriver

我的刮板工作正常,但是我无法弄清楚为什么请求没有通过我设置的代理。 我尝试了三种发现的方法,但是没有一种起作用。 也许这是我应该检查的服务器设置?

这是我没有运气尝试过的三个选择。

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   

1 个答案:

答案 0 :(得分:-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