最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 14:15:16
文档

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

详解在selenium中设置代理ip方法:Firefox中设置代理ipmethod_1from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', 1) profile.set_preference('net
推荐度:
导读详解在selenium中设置代理ip方法:Firefox中设置代理ipmethod_1from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', 1) profile.set_preference('net

# 注意授权
# 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 webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
proxy = Proxy(
 {
 'proxyType': 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_source
driver.close()
方法二:
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
proxy = 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
driver.close()
方法三(动态切换ip):
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType
proxy = 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
# 再新建一个ip
proxy = 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_source
driver.close()

文档

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

详解在selenium中设置代理ip方法:Firefox中设置代理ipmethod_1from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', 1) profile.set_preference('net
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top