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

查看:105
本文介绍了在代理服务器后运行Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用selenium在python中自动进行浏览器模拟和Web抓取,对我来说效果很好.但是现在,我必须在代理服务器后运行它.因此,现在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)

另请参阅相关主题:

  • how do i set proxy for chrome in python webdriver
  • Selenium using Python: enter/provide http proxy password for firefox
  • Running Selenium Webdriver with a proxy in Python
  • http://krosinski.blogspot.ru/2012/11/selenium-firefox-webdriver-and-proxies.html

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

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