我一直在使用 selenium 在 python 中进行自动浏览器模拟和网络抓取,它对我来说效果很好。但是现在,我必须在代理服务器后面运行它。所以现在 selenium 打开窗口但无法打开请求的页面,因为打开的浏览器上没有设置代理设置。当前代码如下(样例):
from selenium import webdriver
sel = webdriver.Firefox()
sel.get('http://www.google.com')
sel.title
sel.quit()
现在如何更改上述代码以与代理服务器一起使用?
最佳答案
您需要设置所需的功能或浏览器配置文件,如下所示:
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)
另见相关主题:
关于python - 在代理服务器后面运行 selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988821/