Firefox中設定代理ip
method_1
from selenium import webdriver profile = 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) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) driver.get('http://httpbin.org/ip')
method_2
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.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 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方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。