【问题标题】:Running selenium behind a proxy server在代理服务器后面运行 selenium
【发布时间】:2013-08-02 01:36:35
【问题描述】:

我一直在使用 selenium 在 python 中进行自动浏览器模拟和网络抓取,它对我来说效果很好。但是现在,我必须在代理服务器后面运行它。因此,现在 selenium 打开窗口但由于未在打开的浏览器上设置代理设置而无法打开请求的页面。当前代码如下(示例):

from selenium import webdriver

sel = webdriver.Firefox()
sel.get('http://www.google.com')
sel.title
sel.quit()

我现在如何更改上述代码以与代理服务器一起使用?

【问题讨论】:

标签: python selenium selenium-webdriver proxy web-scraping


【解决方案1】:
def install_proxy(PROXY_HOST,PROXY_PORT):
    fp = webdriver.FirefoxProfile()
    print PROXY_PORT
    print PROXY_HOST
    fp.set_preference("network.proxy.type", 1)
    fp.set_preference("network.proxy.http",PROXY_HOST)
    fp.set_preference("network.proxy.http_port",int(PROXY_PORT))
    fp.set_preference("network.proxy.https",PROXY_HOST)
    fp.set_preference("network.proxy.https_port",int(PROXY_PORT))
    fp.set_preference("network.proxy.ssl",PROXY_HOST)
    fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT))  
    fp.set_preference("network.proxy.ftp",PROXY_HOST)
    fp.set_preference("network.proxy.ftp_port",int(PROXY_PORT))   
    fp.set_preference("network.proxy.socks",PROXY_HOST)
    fp.set_preference("network.proxy.socks_port",int(PROXY_PORT))   
    fp.set_preference("general.useragent.override","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A")
    fp.update_preferences()
    return webdriver.Firefox(firefox_profile=fp)

【讨论】:

    【解决方案2】:

    您需要设置所需的功能或浏览器配置文件,如下所示:

    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.http", "proxy.server.address")
    profile.set_preference("network.proxy.http_port", "port_number")
    profile.update_preferences()
    driver = webdriver.Firefox(firefox_profile=profile)
    

    另见相关主题:

    【讨论】:

    • 我尝试了你的建议,但仍然无法通过代理服务器
    • 我在进行首选项更新后检查了 selenium 打开的浏览器设置。实际上的问题是,它没有正确设置http_port(并将其保留为0),因此它没有连接。端口设置有问题吗?
    • 嗯,可以试试把它设为数字(不是字符串)吗?
    • 我犯了一个愚蠢的错误。基本上,您不需要将端口号放在大括号下。现在效果很好。
    • 如何为 phantomjs 做同样的事情。基本上,我想将 phantomjs 用于同一代理服务器,而不是 `profile = webdriver.FirefoxProfile() profile.set_proxy(proxy.selenium_proxy())`。
    【解决方案3】:

    这样就可以了:

    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/")
    

    【讨论】:

    • 如何添加用户:pass
    • 重要提示:如果proxyHost是主机名,则不应包含"http://"前缀
    • @UmairAyub 你找到添加user:pass的解决方案了吗?
    【解决方案4】:

    Selenium 官方文档 (http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#using-a-proxy) 提供了关于使用代理的清晰且有用的指南。 对于 Firefox(这是您的示例代码中选择的浏览器),您应该执行以下操作:

    from selenium import webdriver
    from selenium.webdriver.common.proxy import *
    
    myProxy = "host:8080"
    
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
        'noProxy': '' # set this value as desired
        })
    
    driver = webdriver.Firefox(proxy=proxy)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-30
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 2017-08-23
      • 2012-04-17
      • 1970-01-01
      相关资源
      最近更新 更多