在代理服务器后面运行 selenium [英] Running selenium behind a proxy server

查看:29
本文介绍了在代理服务器后面运行 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)

另见相关主题:

这篇关于在代理服务器后面运行 selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆