Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在代理服务器后面运行selenium_Python_Selenium_Selenium Webdriver_Proxy_Web Scraping - Fatal编程技术网

Python 在代理服务器后面运行selenium

Python 在代理服务器后面运行selenium,python,selenium,selenium-webdriver,proxy,web-scraping,Python,Selenium,Selenium Webdriver,Proxy,Web Scraping,我一直在使用selenium进行python中的自动浏览器模拟和web抓取,这对我来说效果很好。但是现在,我必须在代理服务器后面运行它。因此,现在selenium将打开窗口,但无法打开请求的页面,因为未在打开的浏览器上设置代理设置。当前代码如下(示例): 如何更改上述代码以使其也能与代理服务器一起使用?您需要设置所需的功能或浏览器配置文件,如下所示: profile = webdriver.FirefoxProfile() profile.set_preference("network.prox

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


如何更改上述代码以使其也能与代理服务器一起使用?

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

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文档()提供了关于使用代理的清晰而有用的指南。 对于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)
这将完成以下工作:

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

我尝试了您的建议,但仍然无法通过代理服务器。在进行首选项更新后,我检查了selenium打开的浏览器设置。实际上,问题是,它没有正确设置http_端口(并将其保留为0),因此它没有连接。端口设置有什么问题吗?嗯,你能试着把它设置成一个数字(不是字符串)吗?我犯了一个愚蠢的错误。基本上,您不需要将端口号放在大括号下。它现在运行良好。如何对phantomjs执行相同的操作。基本上,我想对同一个代理服务器使用phantomjs,而不是'profile=webdriver.FirefoxProfile()profile.set_proxy(proxy.selenium_proxy())`。您可以尝试这样做如何添加user:pass重要注意:如果
proxyHost
是主机名,则不应包含“http://”prefix@UmairAyub您找到添加用户:pass的解决方案了吗?
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/")
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)