Running selenium behind a proxy server

前端 未结 4 1726
一生所求
一生所求 2020-12-13 15:38

I have been using selenium for automatic browser simulations and web scraping in python and it has worked well for me. But now, I have to run it behind a proxy server. So no

4条回答
  •  误落风尘
    2020-12-13 16:20

    This will do the job:

    import selenium
    from selenium.webdriver.common.proxy import *
    
    proxyHost = "my.proxy.host or IP"
    proxyPort = "55555"
    
    fp = webdriver.FirefoxProfile()
    fp.set_preference("network.proxy.type", 1)
    #fp.set_preference("network.proxy.http", proxyHost) #HTTP PROXY
    #fp.set_preference("network.proxy.http_port", int(proxyPort))
    #fp.set_preference("network.proxy.ssl", proxyHost) #SSL  PROXY
    #fp.set_preference("network.proxy.ssl_port", int(proxyPort))
    fp.set_preference('network.proxy.socks', proxyHost) #SOCKS PROXY
    fp.set_preference('network.proxy.socks_port', int(proxyPort))
    fp.update_preferences()
    
    driver = webdriver.Firefox(firefox_profile=fp)
    
    driver.get("http://www.whatismyip.com/")
    

提交回复
热议问题