在代理服务器后面运行 selenium
问题描述:
我一直在使用 selenium 在 python 中进行自动浏览器模拟和网络抓取,它对我来说效果很好.但是现在,我必须在代理服务器后面运行它.所以现在 selenium 打开窗口但无法打开请求的页面,因为在打开的浏览器上没有设置代理设置.当前代码如下(样例):
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 now selenium open up the window but could not open the requested page because of proxy settings not set on the opened browser. Current code is as follows (sample):
from selenium import webdriver
sel = webdriver.Firefox()
sel.get('http://www.google.com')
sel.title
sel.quit()
我现在如何更改上述代码以与代理服务器一起使用?
How do I change the above code to work with proxy server now as well?
答
您需要设置所需的功能或浏览器配置文件,如下所示:
You need to set desired capabilities or browser profile, like this:
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)
另见相关主题: