• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解在selenium中设置代理ip方法

python 搞代码 4年前 (2022-01-09) 47次浏览 已收录 0个评论

Firefox中设置代理ip

method_1

from selenium import webdriverprofile = webdriver.FirefoxProfile()profile.set_preference('network.proxy.type', 1)profile.set_preference('network.proxy.http', '127.0.0.1')profile.set_preference('network.proxy.http_port', 17890)  # intprofile.update_preferences()driver = webdriver.Firefox(firefox_profile=profile)driver.get('http://httpbin.org/ip')

method_2

from selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy import ProxyTypeproxy = Proxy(    {        # 'proxyType': ProxyType.MANUAL,  # 用不用都行        'httpProxy': get_proxy_ip_port()    })driver = webdriver.Firefox(proxy=proxy)driver.get('http://httpbin.org/ip')

phantomjs中设置代理ip

方法一:太不优雅(还是看方法二吧)

在phantomjs中不能像上面的Firefox的method2一样传入proxy
phantomjs和Firefox均继承自WebDriver,父类WebDriver可以传入proxy
phantomjs在初始化WebDriver时就没有留proxy参数
所以可以像下图一样改一下phantomjs类的源码,就可以在phantomjs中传入proxy参数了

# 注意授权# Licensed to the Software Freedom Conservancy (SFC) under one# or more contributor license agreements.  See the NOTICE file# distributed with this work for additional information# regarding copyright ownership.  The SFC licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License.  You may obtain a copy of the License at##   http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied.  See the License for the# specific language governing permissions and limitations# under the License.
下面才是示例
from selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy import ProxyTypeproxy = Proxy(    {        'prox<b>本文来源gao@dai!ma.com搞$代^码!网7</b>yType': ProxyType.MANUAL,        'httpProxy': get_proxy_ip_port()    })driver = webdriver.PhantomJS(    executable_path="/path/of/phantomjs",    proxy=proxy    )driver.get('http://httpbin.org/ip')print driver.page_sourcedriver.close()
方法二:
from selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy import ProxyTypeproxy = Proxy(    {        'proxyType': ProxyType.MANUAL,        'httpProxy': 'ip:port'  # 代理ip和端口    })# 新建一个“期望技能”,哈哈desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()# 把代理ip加入到技能中proxy.add_to_capabilities(desired_capabilities)driver = webdriver.PhantomJS(    executable_path="/path/of/phantomjs",    desired_capabilities=desired_capabilities    )driver.get('http://httpbin.org/ip')print driver.page_sourcedriver.close()
方法三(动态切换ip):
from selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy import ProxyTypeproxy = Proxy(    {        'proxyType': ProxyType.MANUAL,        'httpProxy': 'ip:port'  # 代理ip和端口    })# 新建一个“期望技能”,哈哈desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()# 把代理ip加入到技能中proxy.add_to_capabilities(desired_capabilities)driver = webdriver.PhantomJS(    executable_path="/path/of/phantomjs",    desired_capabilities=desired_capabilities)# 测试一下driver.get('http://httpbin.org/ip')print driver.page_source# 现在开始切换ip# 再新建一个ipproxy = Proxy(    {        'proxyType': ProxyType.MANUAL,        'httpProxy': 'ip:port'  # 代理ip和端口    })# 再新建一个“期望技能”,()desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()# 把代理ip加入到技能中proxy.add_to_capabilities(desired_capabilities)# 新建一个会话,并把技能传入driver.start_session(desired_capabilities)driver.get('http://httpbin.org/ip')print driver.page_sourcedriver.close()

以上就是详解在selenium中设置代理ip方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解在selenium中设置代理ip方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址