python - 在代理服务器后面运行 selenium

标签 python selenium selenium-webdriver proxy web-scraping

我一直在使用 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/

相关文章:

java - 如何在网页未完全加载时强制WebDriver执行命令?

java - 如果元素未存储在数组中,如何从下拉列表中单击选项

python - tensorflow.python.framework.errors_impl.InvalidArgumentError : Invalid argument: assertion failed:

python argparse 数组而不是 str

python - 如何找到大于 n 的连续值的数量,从最近的日期回溯

java - 如何单击 Selenium 中的组合框搜索下拉菜单?

java - 如果在设定的时间段后无法找到页面上的文本,Selenium Webdriver Java 代码将显示消息

python - python中的递归计数器解决方案

java - 如何在不使用 for 循环的情况下转储 <WebElement> 列表的内容

java - Selenium - 使用 xpath 或 cssSelector 查找元素