toi*_*ing 5 python proxy selenium python-3.x
我试图通过selenium访问一个网站,这个网站在我居住的国家被阻止.我在python中使用selenium并使用代理来执行此操作.但是,我注意到,一旦我检查打开的selenium浏览器的IP,它就会显示我的真实IP而不是代理.这有点令人困惑,因为我设法通过以下驱动程序设置访问此站点一次,但它已经不再工作了.
fp = webdriver.FirefoxProfile()
PROXY_PORT = config['DEFAULT']['PROXY_PORT']
PROXY_HOST = config['DEFAULT']['PROXY_HOST']
fp.set_preference('network.proxy.type', 0)
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", "whater_useragent")
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()
driver = webdriver.Firefox(firefox_profile=fp)
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试使用此方法分配代理,但它没有任何更改.
PROXY_PORT = config['DEFAULT']['PROXY_PORT']
PROXY_HOST = config['DEFAULT']['PROXY_HOST']
myProxy = PROXY_HOST + ':' + PROXY_PORT
proxy = webdriver.common.proxy.Proxy({'proxyType':webdriver.common.proxy.ProxyType.MANUAL,
'httpProxy':myProxy,
'ftpProxy':myProxy,
'sslProxy':myProxy})
driver = webdriver.Firefox(proxy=proxy)
Run Code Online (Sandbox Code Playgroud)
我需要的是打开的firefox浏览器中的IP是代理的IP.如何才能做到这一点?我不知道为什么它起初工作现在它没有.请解释.
使用 Chrome,非常简单:
options = webdriver.ChromeOptions()
options.add_argument('='.join(['--proxy-server', "http://localhost:8888/"]))
Run Code Online (Sandbox Code Playgroud)
您还可以尝试使用插件/扩展来执行此操作。我在这两种方面都取得了成功:)
如果您需要代理服务器: https: //github.com/ochen1/pyproxy