小编典典

在代理服务器后运行selenium

selenium

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

from selenium import webdriver

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

如何更改上面的代码以立即与代理服务器一起使用?


阅读 324

收藏
2020-06-26

共1个答案

小编典典

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

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)
2020-06-26